Search in sources :

Example 96 with FocusEvent

use of org.eclipse.swt.events.FocusEvent in project tdi-studio-se by Talend.

the class JSONFileOutputStep1Form method addFieldsListeners.

@Override
protected void addFieldsListeners() {
    jsonFilePath.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            if (jsonFilePath.getResult() == null) {
                return;
            }
            String text = jsonFilePath.getText();
            if (isContextMode()) {
                ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connectionItem.getConnection(), true);
                text = TalendQuoteUtils.removeQuotes(ConnectionContextHelper.getOriginalValue(contextType, text));
            }
            // getConnection().setJSONFilePath(PathUtils.getPortablePath(JSONXsdFilePath.getText()));
            if (JSONFileOutputStep1Form.this.tempPath == null) {
                JSONFileOutputStep1Form.this.tempPath = JSONUtil.changeJsonToXml(text);
            }
            File file = new File(text);
            if (file.exists()) {
                List<ATreeNode> treeNodes = new ArrayList<ATreeNode>();
                valid = treePopulator.populateTree(JSONUtil.changeJsonToXml(text), treeNode);
                checkFieldsValue();
                if (!valid) {
                    return;
                }
                if (treeNodes.size() > 0) {
                    treeNode = treeNodes.get(0);
                }
                updateConnection(text);
            }
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    });
    jsonFilePath.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent event) {
            String text = jsonFilePath.getText();
            if (isContextMode()) {
                ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connectionItem.getConnection(), true);
                text = TalendQuoteUtils.removeQuotes(ConnectionContextHelper.getOriginalValue(contextType, text));
            }
            if (getConnection().getJSONFilePath() != null && !getConnection().getJSONFilePath().equals(text)) {
                getConnection().getLoop().clear();
                getConnection().getRoot().clear();
                getConnection().getGroup().clear();
                xsdPathChanged = true;
            } else {
                xsdPathChanged = false;
            }
            if (Path.fromOSString(jsonFilePath.getText()).toFile().isFile()) {
                getConnection().setJSONFilePath(PathUtils.getPortablePath(jsonFilePath.getText()));
            } else {
                getConnection().setJSONFilePath(jsonFilePath.getText());
            }
            // updateConnection(text);
            StringBuilder fileContent = new StringBuilder();
            BufferedReader in = null;
            File file = null;
            if (tempJSONPath != null && getConnection().getFileContent() != null && getConnection().getFileContent().length > 0 && !isModifing) {
                file = new File(tempJSONPath);
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e2) {
                        ExceptionHandler.process(e2);
                    }
                    FileOutputStream outStream;
                    try {
                        outStream = new FileOutputStream(file);
                        outStream.write(getConnection().getFileContent());
                        outStream.close();
                    } catch (FileNotFoundException e1) {
                        ExceptionHandler.process(e1);
                    } catch (IOException e) {
                        ExceptionHandler.process(e);
                    }
                }
            } else {
                file = new File(text);
            }
            String str;
            try {
                Charset guessCharset = CharsetToolkit.guessEncoding(file, 4096);
                in = new BufferedReader(new InputStreamReader(new FileInputStream(file), guessCharset.displayName()));
                while ((str = in.readLine()) != null) {
                    fileContent.append(str + "\n");
                    // for encoding
                    if (str.contains("encoding")) {
                        String regex = "^<\\?JSON\\s*version=\\\"[^\\\"]*\\\"\\s*encoding=\\\"([^\\\"]*)\\\"\\?>$";
                        Perl5Compiler compiler = new Perl5Compiler();
                        Perl5Matcher matcher = new Perl5Matcher();
                        Pattern pattern = null;
                        try {
                            pattern = compiler.compile(regex);
                            if (matcher.contains(str, pattern)) {
                                MatchResult matchResult = matcher.getMatch();
                                if (matchResult != null) {
                                    encoding = matchResult.group(1);
                                }
                            }
                        } catch (MalformedPatternException malE) {
                            ExceptionHandler.process(malE);
                        }
                    }
                }
                fileContentText.setText(new String(fileContent));
            } catch (Exception e) {
                String msgError = "File" + " \"" + jsonFilePath.getText().replace("\\\\", "\\") + "\"\n";
                if (e instanceof FileNotFoundException) {
                    msgError = msgError + "is not found";
                } else if (e instanceof EOFException) {
                    msgError = msgError + "have an incorrect character EOF";
                } else if (e instanceof IOException) {
                    msgError = msgError + "is locked by another soft";
                } else {
                    msgError = msgError + "doesn't exist";
                }
                fileContentText.setText(msgError);
                if (!isReadOnly()) {
                    updateStatus(IStatus.ERROR, msgError);
                }
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception exception) {
                    ExceptionHandler.process(exception);
                }
            }
            if (getConnection().getEncoding() == null || "".equals(getConnection().getEncoding())) {
                getConnection().setEncoding(encoding);
                if (encoding != null && !"".equals(encoding)) {
                    encodingCombo.setText(encoding);
                } else {
                    encodingCombo.setText("UTF-8");
                }
            }
            // }
            if (file.exists()) {
                valid = treePopulator.populateTree(JSONUtil.changeJsonToXml(text), treeNode);
                updateConnection(text);
            }
            checkFieldsValue();
            isModifing = true;
        }
    });
    encodingCombo.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            getConnection().setEncoding(encodingCombo.getText());
            checkFieldsValue();
        }
    });
    commonNodesLimitation.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            String str = commonNodesLimitation.getText();
            if ((!str.matches("\\d+")) || (Integer.valueOf(str) < 0)) {
                commonNodesLimitation.setText(String.valueOf(treePopulator.getLimit()));
                labelLimitation.setToolTipText(MessageFormat.format(Messages.JSONLimitToolTip, commonNodesLimitation.getText()));
            } else {
                treePopulator.setLimit(Integer.valueOf(str));
                labelLimitation.setToolTipText(MessageFormat.format(Messages.JSONLimitToolTip, str));
            }
            if (JSONFileOutputStep1Form.this.tempPath == null) {
                JSONFileOutputStep1Form.this.tempPath = JSONUtil.changeJsonToXml(jsonFilePath.getText());
            }
            File file = new File(JSONFileOutputStep1Form.this.tempPath);
            if (file.exists()) {
                valid = treePopulator.populateTree(JSONFileOutputStep1Form.this.tempPath, treeNode);
            }
            checkFieldsValue();
        }
    });
    commonNodesLimitation.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
        }

        @Override
        public void focusLost(FocusEvent e) {
            commonNodesLimitation.setText(String.valueOf(TreePopulator.getLimit()));
            labelLimitation.setToolTipText(MessageFormat.format(Messages.JSONLimitToolTip, commonNodesLimitation.getText()));
        }
    });
    outputFilePath.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            getConnection().setOutputFilePath(PathUtils.getPortablePath(outputFilePath.getText()));
            checkFieldsValue();
        }
    });
}
Also used : ModifyListener(org.eclipse.swt.events.ModifyListener) FileNotFoundException(java.io.FileNotFoundException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult) FocusEvent(org.eclipse.swt.events.FocusEvent) ModifyEvent(org.eclipse.swt.events.ModifyEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) EOFException(java.io.EOFException) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) InputStreamReader(java.io.InputStreamReader) Charset(java.nio.charset.Charset) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PersistenceException(org.talend.commons.exception.PersistenceException) ATreeNode(org.talend.datatools.xml.utils.ATreeNode) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) RecordFile(orgomg.cwm.resource.record.RecordFile) File(java.io.File) FocusListener(org.eclipse.swt.events.FocusListener) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 97 with FocusEvent

use of org.eclipse.swt.events.FocusEvent in project tesb-studio-se by Talend.

the class SearchControl method initialize.

private void initialize(int style) {
    text = new Text(this, style);
    text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    text.setMessage("search what you want");
    text.addFocusListener(new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent e) {
            text.selectAll();
        }
    });
    text.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseDown(MouseEvent e) {
            text.selectAll();
        }
    });
    clear = new ToolBar(this, SWT.FLAT);
    ToolItem item = new ToolItem(clear, SWT.PUSH);
    item.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            text.setText("");
        }
    });
    Point computeSize = clear.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    int height = computeSize.y + 2;
    arcSize = height > arcSize ? height : arcSize;
    GridLayout layout = new GridLayout(2, false);
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginBottom = 0;
    layout.marginHeight = 0;
    layout.marginLeft = marginHorizon;
    layout.marginRight = marginHorizon;
    layout.marginTop = 1;
    layout.marginWidth = 0;
    setLayout(layout);
    setBackground(backgroundColor);
}
Also used : FocusAdapter(org.eclipse.swt.events.FocusAdapter) MouseEvent(org.eclipse.swt.events.MouseEvent) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) MouseAdapter(org.eclipse.swt.events.MouseAdapter) Text(org.eclipse.swt.widgets.Text) Point(org.eclipse.swt.graphics.Point) FocusEvent(org.eclipse.swt.events.FocusEvent) Point(org.eclipse.swt.graphics.Point) GridLayout(org.eclipse.swt.layout.GridLayout) GridData(org.eclipse.swt.layout.GridData) ToolBar(org.eclipse.swt.widgets.ToolBar) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ToolItem(org.eclipse.swt.widgets.ToolItem)

Example 98 with FocusEvent

use of org.eclipse.swt.events.FocusEvent in project bndtools by bndtools.

the class PkgPatternsListPart method createSection.

protected void createSection(Section section, FormToolkit toolkit) {
    Composite composite = toolkit.createComposite(section);
    section.setClient(composite);
    ToolBar toolbar = new ToolBar(section, SWT.FLAT);
    section.setTextClient(toolbar);
    final ToolItem addItem = new ToolItem(toolbar, SWT.PUSH);
    addItem.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ADD));
    addItem.setToolTipText("Add");
    final ToolItem insertItem = new ToolItem(toolbar, SWT.PUSH);
    insertItem.setImage(imgInsert);
    insertItem.setToolTipText("Insert");
    insertItem.setEnabled(false);
    final ToolItem removeItem = new ToolItem(toolbar, SWT.PUSH);
    removeItem.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE));
    removeItem.setDisabledImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE_DISABLED));
    removeItem.setToolTipText("Remove");
    removeItem.setEnabled(false);
    Table table = toolkit.createTable(composite, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
    viewer = new TableViewer(table);
    viewer.setUseHashlookup(false);
    viewer.setContentProvider(new ArrayContentProvider());
    viewer.setLabelProvider(labelProvider);
    toolbar = new ToolBar(composite, SWT.FLAT | SWT.HORIZONTAL | SWT.RIGHT);
    final ToolItem btnMoveUp = new ToolItem(toolbar, SWT.PUSH);
    btnMoveUp.setText("Up");
    btnMoveUp.setImage(imgUp);
    btnMoveUp.setEnabled(false);
    final ToolItem btnMoveDown = new ToolItem(toolbar, SWT.PUSH);
    btnMoveDown.setText("Down");
    btnMoveDown.setImage(imgDown);
    btnMoveDown.setEnabled(false);
    // Listeners
    table.addFocusListener(new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent e) {
            ISelection selection = viewer.getSelection();
            if (!selection.isEmpty())
                managedForm.fireSelectionChanged(PkgPatternsListPart.this, selection);
        }
    });
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            List<C> oldSelection = selection;
            IStructuredSelection structSel = (IStructuredSelection) event.getSelection();
            @SuppressWarnings("unchecked") List<C> newSelection = structSel.toList();
            selection = newSelection;
            propChangeSupport.firePropertyChange(PROP_SELECTION, oldSelection, selection);
            managedForm.fireSelectionChanged(PkgPatternsListPart.this, event.getSelection());
            boolean enabled = !viewer.getSelection().isEmpty();
            insertItem.setEnabled(enabled);
            removeItem.setEnabled(enabled);
            btnMoveUp.setEnabled(enabled);
            btnMoveDown.setEnabled(enabled);
        }
    });
    viewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, new Transfer[] { LocalSelectionTransfer.getTransfer(), ResourceTransfer.getInstance(), TextTransfer.getInstance() }, new PackageDropAdapter<C>(viewer) {

        @Override
        protected C createNewEntry(String packageName) {
            return newHeaderClause(packageName);
        }

        @Override
        protected void addRows(int index, Collection<C> rows) {
            doAddClauses(rows, index, true);
        }

        @Override
        protected int indexOf(Object object) {
            return clauses.indexOf(object);
        }
    });
    table.addKeyListener(new KeyAdapter() {

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.character == SWT.DEL) {
                doRemoveSelectedClauses();
            } else if (e.character == '+') {
                doAddClausesAfterSelection(generateClauses());
            }
        }
    });
    addItem.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            doAddClausesAfterSelection(generateClauses());
        }
    });
    insertItem.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            doInsertClausesAtSelection(generateClauses());
        }
    });
    removeItem.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            doRemoveSelectedClauses();
        }
    });
    btnMoveUp.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            doMoveUp();
        }
    });
    btnMoveDown.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            doMoveDown();
        }
    });
    // Layout
    GridLayout layout;
    layout = new GridLayout(1, false);
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    layout.verticalSpacing = 0;
    layout.horizontalSpacing = 0;
    composite.setLayout(layout);
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.widthHint = 75;
    gd.heightHint = 75;
    table.setLayoutData(gd);
    toolbar.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
}
Also used : FocusAdapter(org.eclipse.swt.events.FocusAdapter) KeyAdapter(org.eclipse.swt.events.KeyAdapter) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) FocusEvent(org.eclipse.swt.events.FocusEvent) KeyEvent(org.eclipse.swt.events.KeyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) ISelection(org.eclipse.jface.viewers.ISelection) SelectionEvent(org.eclipse.swt.events.SelectionEvent) List(java.util.List) ArrayList(java.util.ArrayList) ToolItem(org.eclipse.swt.widgets.ToolItem) Table(org.eclipse.swt.widgets.Table) Composite(org.eclipse.swt.widgets.Composite) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ToolBar(org.eclipse.swt.widgets.ToolBar) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) GridData(org.eclipse.swt.layout.GridData) TableViewer(org.eclipse.jface.viewers.TableViewer)

Aggregations

FocusEvent (org.eclipse.swt.events.FocusEvent)98 FocusAdapter (org.eclipse.swt.events.FocusAdapter)55 SelectionEvent (org.eclipse.swt.events.SelectionEvent)50 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)42 FocusListener (org.eclipse.swt.events.FocusListener)40 GridData (org.eclipse.swt.layout.GridData)34 Text (org.eclipse.swt.widgets.Text)30 GridLayout (org.eclipse.swt.layout.GridLayout)29 Composite (org.eclipse.swt.widgets.Composite)28 Label (org.eclipse.swt.widgets.Label)23 Menu (org.eclipse.swt.widgets.Menu)21 Button (org.eclipse.swt.widgets.Button)19 MenuManager (org.eclipse.jface.action.MenuManager)18 ModifyEvent (org.eclipse.swt.events.ModifyEvent)17 ModifyListener (org.eclipse.swt.events.ModifyListener)17 Group (org.eclipse.swt.widgets.Group)17 Point (org.eclipse.swt.graphics.Point)16 Combo (org.eclipse.swt.widgets.Combo)15 MenuItem (org.eclipse.swt.widgets.MenuItem)15 KeyEvent (org.eclipse.swt.events.KeyEvent)12