数据记录文件转换工具

    private void creatToolArea(final Shell shell) {

        shell.setLayout(new GridLayout(1, false));
        final Composite comp = new Composite(shell, SWT.NONE);
        final GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
        comp.setLayoutData(gridData);
        comp.setLayout(new GridLayout(3, false));

        final GridData gridData3 = new GridData(SWT.FILL, SWT.FILL, true, false, 3, 1);
        final GridData gridData1 = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);

        final Label lblToolName = new Label(comp, SWT.CENTER);
        lblToolName.setFont(new Font(Display.getDefault(), "Tahoma", 10, SWT.BOLD));
        lblToolName.setText(Messages.ToolConvert_Title);
        lblToolName.setLayoutData(gridData3);
        lblWarn = new Label(comp, SWT.CENTER);
        lblWarn.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
        lblWarn.setLayoutData(gridData3);
        lblWarn.setText(StringUtil.EMPTY);

        final Label lblRecordPath = new Label(comp, SWT.NONE);
        lblRecordPath.setText(Messages.ToolConvert_RecordFilePath);
        txtRecordPath = new Text(comp, SWT.BORDER);
        txtRecordPath.setLayoutData(gridData1);
        btnRecordPath = new Button(comp, SWT.NONE);
        btnRecordPath.setText(Messages.ToolConvert_ButtonPath);

        final Label lblPreloadPath = new Label(comp, SWT.NONE);
        lblPreloadPath.setText(Messages.ToolConvert_PreLoadPath);
        txtPreloadPath = new Text(comp, SWT.BORDER);
        txtPreloadPath.setLayoutData(gridData1);
        btnPreloadPath = new Button(comp, SWT.NONE);
        btnPreloadPath.setText(Messages.ToolConvert_ButtonPath);

        tv = new TableViewer(comp, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
        final Table table = tv.getTable();
        table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
        table.setLinesVisible(true);
        table.setHeaderVisible(true);
        final TableColumn columnX = new TableColumn(table, SWT.NONE);
        columnX.setText(Messages.ToolConvert_TableColum_X);
        columnX.setWidth(130);
        final TableColumn columnY = new TableColumn(table, SWT.NONE);
        columnY.setText(Messages.ToolConvert_TableColum_Y);
        columnY.setWidth(130);
        final TableColumn columnName = new TableColumn(table, SWT.NONE);
        columnName.setText(Messages.ToolConvert_TableColum_Name);
        columnName.setWidth(160);
        tv.setColumnProperties(columnProperties);
        cellEditor = new CellEditor[3];
        tv.setCellModifier(new MyICellModifier());
        tv.setCellEditors(cellEditor);
        tv.setContentProvider(new ArrayContentProvider());
        tv.setLabelProvider(new ToolLableProvider());

        final Composite comBtn = new Composite(comp, SWT.NONE);
        comBtn.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false, 3, 1));
        comBtn.setLayout(new FillLayout());
        btnAdd = new Button(comBtn, SWT.NONE);
        btnAdd.setText(Messages.ToolConvert_Button_Add);
        btnAdd.setEnabled(false);
        btnRemove = new Button(comBtn, SWT.NONE);
        btnRemove.setEnabled(false);
        btnRemove.setText(Messages.ToolConvert_Button_Remove);
        btnGenerate = new Button(comBtn, SWT.NONE);
        btnGenerate.setText(Messages.ToolConvert_Button_Generate);
        btnGenerate.setEnabled(false);

        refreshInput();
        addButtonListener();
        lblWarn.setText(StringUtil.EMPTY);

    }

    private void addButtonListener() {
        btnRecordPath.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(final SelectionEvent e) {
                final FileDialog dialog = new FileDialog(btnRecordPath.getShell(), SWT.OPEN);
                dialog.setFilterExtensions(new String[] { "*.txt", "*.*" });
                dialog.setFilterPath(Platform.getLocation() + StringUtil.EMPTY);
                final String path = dialog.open();
                if (path != null) {
                    txtRecordPath.setText(path);
                }
                super.widgetSelected(e);
            }
        });

        btnPreloadPath.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(final SelectionEvent e) {
                final FileDialog dialog = new FileDialog(btnPreloadPath.getShell(), SWT.SAVE);
                dialog.setFilterExtensions(new String[] { "*.*" });
                dialog.setFilterPath(FileSystemView.getFileSystemView().getHomeDirectory() + StringUtil.EMPTY);
                final String path = dialog.open();
                if (path != null) {
                    txtPreloadPath.setText(path);
                }
                super.widgetSelected(e);
            }
        });

        btnAdd.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(final SelectionEvent e) {
                lblWarn.setText(StringUtil.EMPTY);
                final PreLoadBean bean = new PreLoadBean(fileHeaders[0], fileHeaders[1], fileHeaders[0] + StringUtil.UNDER_LINE + fileHeaders[1]);
                input.add(bean);
                refreshInput();
                btnRemove.setEnabled(true);
                super.widgetSelected(e);
            }
        });

        btnRemove.addSelectionListener(new SelectionAdapter() {

            @SuppressWarnings("unchecked")
            @Override
            public void widgetSelected(final SelectionEvent e) {
                lblWarn.setText(StringUtil.EMPTY);
                final IStructuredSelection s = (IStructuredSelection) tv.getSelection();
                if (!s.isEmpty()) {
                    for (final Iterator<PreLoadBean> iterator = s.iterator(); iterator.hasNext();) {
                        final PreLoadBean bean = iterator.next();
                        input.remove(bean);
                    }
                }
                refreshInput();
                if (input.size() == 0) {
                    btnRemove.setEnabled(false);
                }
            }
        });

        btnGenerate.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(final SelectionEvent e) {
                lblWarn.setText(Messages.ToolConvert_Warn_Converting);
                readRecordFiles();
                generatePreloadFiles();
                lblWarn.setText(Messages.ToolConvert_Warn_Converted);
            }
        });

        txtRecordPath.addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(final ModifyEvent e) {
                final Text text = (Text) e.widget;
                refreshRecordFile(text.getText());
                input.clear();
                refreshInput();
            }
        });

        txtPreloadPath.addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(final ModifyEvent e) {
                final String path = txtPreloadPath.getText();
                final File file = new File(path);
                if (!file.exists()) {
                    file.mkdir();
                }
                if (!file.getAbsolutePath().equals(txtPreloadPath.getText())) {
                    txtPreloadPath.setText(file.getAbsolutePath());
                }
                isValid();
            }
        });

        txtRecordPath.getShell().addDisposeListener(new DisposeListener() {

            @Override
            public void widgetDisposed(final DisposeEvent e) {
                input.clear();
            }
        });
    }


/**
     * 更新数据记录文件时,同时更新table中的下拉框可选择的X轴、Y轴列的表头
     */
    private void refreshRecordFile(final String path) {
        final File recordFile = new File(path);
        if (recordFile.exists()) {
            lblWarn.setText(StringUtil.EMPTY);
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new FileReader(recordFile));
                final String title = reader.readLine();
                if (title.contains(STR_TABLE) && (fileHeader = title.split(STR_TABLE))[0].equals("index")) {
                    fileHeaders = new String[fileHeader.length - 1];
                    for (int i = 1; i < fileHeader.length; i++) {
                        fileHeaders[i - 1] = fileHeader[i];
                    }
                    cellEditor[0] = new ComboBoxCellEditor(tv.getTable(), fileHeaders, SWT.READ_ONLY);
                    cellEditor[1] = new ComboBoxCellEditor(tv.getTable(), fileHeaders, SWT.READ_ONLY);
                    cellEditor[2] = new TextCellEditor(tv.getTable());
                    btnAdd.setEnabled(true);
                } else {
                    lblWarn.setText(Messages.ToolConvert_Warn_RecordFile_format);
                    btnAdd.setEnabled(false);
                }
            } catch (final FileNotFoundException e) {
                HigaleUIPlugin.log(e.getMessage(), IStatus.ERROR);
            } catch (final IOException e) {
                HigaleUIPlugin.log(e.getMessage(), IStatus.ERROR);
            }
        } else {
            lblWarn.setText(Messages.ToolConvert_Warn_RecordFile_NotExist);
            btnAdd.setEnabled(false);
        }
    }

    /**
     * 把用户 选择的 某些列 数据读进内存
     */
    private void readRecordFiles() {
        mapHeaderValue = new HashMap<String, List<String>>();
        for (final PreLoadBean bean : input) {
            if (!mapHeaderValue.containsKey(bean.getxAxis())) {
                mapHeaderValue.put(bean.getxAxis(), new ArrayList<String>());
            }
            if (!mapHeaderValue.containsKey(bean.getyAxis())) {
                mapHeaderValue.put(bean.getyAxis(), new ArrayList<String>());
            }
        }
        // 用户选择的 第几列index、对应的列名
        final Map<Integer, String> mapIndexHeader = new HashMap<Integer, String>();
        // 用户选择的列的index
        final List<Integer> listIndex = new ArrayList<Integer>();
        int index = 0;
        for (final String header : fileHeader) {
            if (mapHeaderValue.containsKey(header)) {
                mapIndexHeader.put(index, header);
                listIndex.add(index);
            }
            index++;
        }
        final File recordFile = new File(txtRecordPath.getText());
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(recordFile));
            String lineStr = reader.readLine();
            while ((lineStr = reader.readLine()) != null) {
                final String[] fileHeadersValues = lineStr.split(STR_TABLE);
                for (final int indexArray : listIndex) {
                    mapHeaderValue.get(mapIndexHeader.get(indexArray)).add(fileHeadersValues[indexArray]);
                }
            }
        } catch (final FileNotFoundException e) {
            HigaleUIPlugin.log(e.getMessage(), IStatus.ERROR);
        } catch (final IOException e) {
            HigaleUIPlugin.log(e.getMessage(), IStatus.ERROR);
        }

    }

    /**
     * 生成 指定 个数 的预设文件
     */
    private void generatePreloadFiles() {
        for (final PreLoadBean bean : input) {
            final File file = new File(txtPreloadPath.getText() + StringUtil.RIGHT_SLASH + bean.getName() + ".txt");
            try {
                final BufferedWriter bufferWriter = new BufferedWriter(new FileWriter(file));
                final StringBuffer buffer = new StringBuffer();
                int lineIndex = 0;
                while (lineIndex < mapHeaderValue.get(bean.getxAxis()).size()) {
                    buffer.append(mapHeaderValue.get(bean.getxAxis()).get(lineIndex) + STR_TABLE);
                    buffer.append(mapHeaderValue.get(bean.getyAxis()).get(lineIndex) + "\r\n");
                    lineIndex++;
                }
                bufferWriter.write(buffer.toString());
                bufferWriter.close();
            } catch (final IOException e) {
                HigaleUIPlugin.log(e.getMessage(), IStatus.ERROR);
                lblWarn.setText(Messages.ToolConvert_Warn_PreloadFile_NotExist);
            }
        }
    }

    private class ToolLableProvider extends LabelProvider implements ITableLabelProvider {

        @Override
        public Image getColumnImage(final Object element, final int columnIndex) {
            return null;
        }

        @Override
        public String getColumnText(final Object element, final int columnIndex) {
            if (element instanceof PreLoadBean) {
                final PreLoadBean bean = (PreLoadBean) element;
                switch (columnIndex) {
                case 0:
                    return bean.getxAxis();
                case 1:
                    return bean.getyAxis();
                case 2:
                    return bean.getName();
                default:
                    break;
                }
            }
            return null;
        }
    }

    private class MyICellModifier implements ICellModifier {

        @Override
        public boolean canModify(final Object element, final String property) {
            return true;
        }

        @Override
        public Object getValue(final Object element, final String property) {
            final PreLoadBean bean = (PreLoadBean) element;
            if (StringUtil.isEquals(property, columnProperties[0])) {
                return getIndex(bean.getxAxis());
            }
            if (StringUtil.isEquals(property, columnProperties[1])) {
                return getIndex(bean.getyAxis());
            }
            if (StringUtil.isEquals(property, columnProperties[2])) {
                return bean.getName();
            }
            return null;
        }

        @Override
        public void modify(final Object element, final String property, final Object value) {
            final TableItem item = (TableItem) element;
            final Object data = item.getData();

            if (data instanceof PreLoadBean) {
                final PreLoadBean bean = (PreLoadBean) data;
                if (StringUtil.isEquals(property, columnProperties[0])) {
                    final Integer index = (Integer) value;
                    if (bean.getName().contains(bean.getxAxis())) {
                        bean.setName(bean.getName().replace(bean.getxAxis(), fileHeaders[index]));
                    }
                    bean.setxAxis(fileHeaders[index]);
                }
                if (StringUtil.isEquals(property, columnProperties[1])) {
                    final Integer index = (Integer) value;
                    if (bean.getName().contains(bean.getyAxis())) {
                        bean.setName(bean.getName().replace(bean.getyAxis(), fileHeaders[index]));
                    }
                    bean.setyAxis(fileHeaders[index]);
                }
                if (StringUtil.isEquals(property, columnProperties[2])) {
                    final String name = (String) value;
                    bean.setName(name);
                }
                refreshInput();
            }
        }

        private int getIndex(final String str) {
            int index = 0;
            for (final String s : fileHeaders) {
                if (s.equals(str)) {
                    return index;
                }
                index++;
            }
            return -2;
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值