Example usage for org.eclipse.jface.dialogs Dialog convertWidthInCharsToPixels

List of usage examples for org.eclipse.jface.dialogs Dialog convertWidthInCharsToPixels

Introduction

In this page you can find the example usage for org.eclipse.jface.dialogs Dialog convertWidthInCharsToPixels.

Prototype

public static int convertWidthInCharsToPixels(FontMetrics fontMetrics, int chars) 

Source Link

Document

Returns the number of pixels corresponding to the width of the given number of characters.

Usage

From source file:com.maccasoft.composer.CommandControl.java

License:Open Source License

void createVolumeRegisterControls(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(1, false);
    layout.marginWidth = layout.marginHeight = 0;
    container.setLayout(layout);/*from w w  w .j a v  a 2s .com*/

    volumeRegister = new Text(container, SWT.BORDER | SWT.CENTER);
    volumeRegister.setToolTipText("Register value (hex)");
    volumeRegister
            .setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 11), SWT.DEFAULT));
    volumeRegister.addFocusListener(textFocusListener);
}

From source file:com.maccasoft.composer.CommandControl.java

License:Open Source License

void createModulationControls(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(4, false);
    layout.marginWidth = layout.marginHeight = 0;
    container.setLayout(layout);//  w  w w. j a v  a  2  s  .co m

    variable = new Text(container, SWT.BORDER | SWT.CENTER);
    variable.setToolTipText("Variable modulation");
    variable.setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 11), SWT.DEFAULT));
    variable.addFocusListener(textFocusListener);

    Label label = new Label(container, SWT.NONE);
    label.setText("Hz");
    label.setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 3), SWT.DEFAULT));

    fixed = new Text(container, SWT.BORDER | SWT.CENTER);
    fixed.setToolTipText("Fixed duty cycle");
    fixed.setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 6), SWT.DEFAULT));
    fixed.addFocusListener(textFocusListener);

    label = new Label(container, SWT.NONE);
    label.setText("%");
}

From source file:com.maccasoft.composer.CommandControl.java

License:Open Source License

void createModulationRegisterControls(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(1, false);
    layout.marginWidth = layout.marginHeight = 0;
    container.setLayout(layout);/* w w  w  . ja va2  s .co m*/

    modulationRegister = new Text(container, SWT.BORDER | SWT.CENTER);
    modulationRegister.setToolTipText("Register value (hex)");
    modulationRegister
            .setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 11), SWT.DEFAULT));
    modulationRegister.addFocusListener(textFocusListener);
}

From source file:com.maccasoft.composer.CommandControl.java

License:Open Source License

void createJumpControls(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(1, false);
    layout.marginWidth = layout.marginHeight = 0;
    container.setLayout(layout);//from w w  w .j a va 2s.c  o m

    steps = new Spinner(container, SWT.BORDER);
    steps.setValues(0, -9999, 9999, 0, 1, 1);
    steps.setToolTipText("Number of instructions");
    steps.setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 5), SWT.DEFAULT));
}

From source file:com.maccasoft.composer.InstrumentChart.java

License:Open Source License

void createBottomControls(Composite parent) {
    Composite group = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(4, false);
    layout.marginWidth = layout.marginHeight = 0;
    group.setLayout(layout);//w  w w  .j a  v a 2 s . c om
    group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    Label label = new Label(group, SWT.NONE);
    label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    label = new Label(group, SWT.NONE);
    label.setText("Duration");

    time = new Spinner(group, SWT.BORDER);
    time.setValues(100, 100, 100000, 0, 100, 100);
    time.setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 6), SWT.DEFAULT));
    time.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (instrument != null) {
                startUpdateThread();
            }
        }
    });

    label = new Label(group, SWT.NONE);
    label.setText("ms.");
}

From source file:com.maccasoft.composer.InstrumentEditor.java

License:Open Source License

void createToolBar(Composite parent) {
    Composite container = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout(2, false);
    layout.marginWidth = layout.marginHeight = 0;
    container.setLayout(layout);/*  ww w .  j a  v  a  2  s.c  o m*/
    container.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

    name = new Text(container, SWT.BORDER);
    name.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    ((GridData) name.getLayoutData()).widthHint = Dialog.convertWidthInCharsToPixels(fontMetrics, 30);
    name.setText(instrument.getName());

    ToolBar toolBar = new ToolBar(container, SWT.FLAT);
    toolBar.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));

    ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
    toolItem.setImage(ImageRegistry.getImageFromResources("document-import.png"));
    toolItem.setToolTipText("Import from file");
    toolItem.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            try {
                handleImportFromFile();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });

    toolItem = new ToolItem(toolBar, SWT.PUSH);
    toolItem.setImage(ImageRegistry.getImageFromResources("document-export.png"));
    toolItem.setToolTipText("Export to file");
    toolItem.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            try {
                handleExportToFile();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
}

From source file:com.maccasoft.composer.InstrumentToolBar.java

License:Open Source License

public InstrumentToolBar(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    GridLayout gridLayout = new GridLayout(3, false);
    gridLayout.marginWidth = gridLayout.marginHeight = 0;
    composite.setLayout(gridLayout);/*from ww w .j  av a 2 s. c o m*/

    shell = parent.getShell();

    GC gc = new GC(parent);
    FontMetrics fontMetrics = gc.getFontMetrics();
    gc.dispose();

    Label label = new Label(composite, SWT.NONE);
    label.setText("Instrument");

    viewer = new ComboViewer(composite, SWT.DROP_DOWN | SWT.READ_ONLY | SWT.NO_FOCUS);
    viewer.getCombo().setVisibleItemCount(20);
    viewer.getCombo()
            .setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 30), SWT.DEFAULT));
    viewer.setContentProvider(new ObservableListContentProvider());
    viewer.setLabelProvider(new LabelProvider() {

        @Override
        public String getText(Object element) {
            return String.format("%s - %s", project.getInstrumentId((Instrument) element), element.toString());
        }
    });

    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            ISelection selection = event.getSelection();
            duplicate.setEnabled(!selection.isEmpty());
            edit.setEnabled(!selection.isEmpty());
            delete.setEnabled(!selection.isEmpty());
        }
    });

    ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.NO_FOCUS);
    ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
    toolItem.setImage(ImageRegistry.getImageFromResources("application_add.png"));
    toolItem.setToolTipText("New instrument");
    toolItem.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            Instrument instrument = new InstrumentBuilder("New instrument") //
                    .setModulation(0, 50) //
                    .setVolume(95) //
                    .setEnvelope(2, 2).repeat(1) //
                    .jump(-1).build();

            InstrumentEditor editor = new InstrumentEditor(shell, instrument);
            editor.setSerialPort(serialPort);
            if (editor.open() == InstrumentEditor.OK) {
                project.add(instrument);
                viewer.setSelection(new StructuredSelection(instrument));
            }
        }
    });

    duplicate = new ToolItem(toolBar, SWT.PUSH);
    duplicate.setImage(ImageRegistry.getImageFromResources("application_double.png"));
    duplicate.setToolTipText("Duplicate instrument");
    duplicate.setEnabled(false);
    duplicate.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection selection = viewer.getStructuredSelection();
            if (selection.isEmpty()) {
                return;
            }
            Instrument selectedInstrument = (Instrument) selection.getFirstElement();

            Instrument instrument = new Instrument(selectedInstrument.getName() + " (1)");
            List<Command> list = new ArrayList<Command>();
            try {
                for (Command cmd : selectedInstrument.getCommands()) {
                    list.add(cmd.clone());
                }
                instrument.setCommands(list);
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            InstrumentEditor editor = new InstrumentEditor(shell, instrument);
            editor.setSerialPort(serialPort);
            if (editor.open() == InstrumentEditor.OK) {
                project.add(instrument);
                viewer.setSelection(new StructuredSelection(instrument));
            }
        }
    });

    edit = new ToolItem(toolBar, SWT.PUSH);
    edit.setImage(ImageRegistry.getImageFromResources("application_edit.png"));
    edit.setToolTipText("Edit instrument");
    edit.setEnabled(false);
    edit.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection selection = viewer.getStructuredSelection();
            if (selection.isEmpty()) {
                return;
            }
            Instrument selectedInstrument = (Instrument) selection.getFirstElement();

            InstrumentEditor editor = new InstrumentEditor(shell, selectedInstrument);
            editor.setSerialPort(serialPort);
            if (editor.open() == InstrumentEditor.OK) {
                viewer.refresh();
                viewer.setSelection(new StructuredSelection(selectedInstrument));
            }
        }
    });

    new ToolItem(toolBar, SWT.SEPARATOR);

    delete = new ToolItem(toolBar, SWT.PUSH);
    delete.setImage(ImageRegistry.getImageFromResources("application_delete.png"));
    delete.setToolTipText("Delete instrument");
    delete.setEnabled(false);
    delete.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection selection = viewer.getStructuredSelection();
            if (selection.isEmpty()) {
                return;
            }

            Instrument selectedInstrument = (Instrument) selection.getFirstElement();
            if (isInstrumentUsed(selectedInstrument)) {
                if (!MessageDialog.openConfirm(shell, Main.APP_TITLE,
                        "The instrument is used in a song.  You really want to delete?")) {
                    return;
                }
            }
            project.remove(selectedInstrument);
        }
    });
}

From source file:com.maccasoft.composer.MusicEditor.java

License:Open Source License

void createHeader(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    GridLayout gridLayout = new GridLayout(12, false);
    gridLayout.marginWidth = gridLayout.marginHeight = 0;
    composite.setLayout(gridLayout);/*from   w  w w  .java2 s . co  m*/
    composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    Label label = new Label(composite, SWT.NONE);
    label.setText("Song");

    songsCombo = new ComboViewer(composite, SWT.DROP_DOWN | SWT.READ_ONLY | SWT.NO_FOCUS);
    songsCombo.getCombo().setVisibleItemCount(20);
    songsCombo.getCombo()
            .setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 30), SWT.DEFAULT));
    songsCombo.setContentProvider(new ObservableListContentProvider());
    songsCombo.setLabelProvider(new LabelProvider());

    ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.NO_FOCUS);

    edit = new ToolItem(toolBar, SWT.PUSH);
    edit.setImage(ImageRegistry.getImageFromResources("application_edit.png"));
    edit.setToolTipText("Rename");
    edit.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            IInputValidator validator = new IInputValidator() {

                @Override
                public String isValid(String newText) {
                    if (newText.length() == 0) {
                        return "";
                    }
                    for (Song song : project.getSongs()) {
                        if (song != currentSong && newText.equalsIgnoreCase(song.getName())) {
                            return "A song with the same title already exists";
                        }
                    }
                    return null;
                }
            };
            InputDialog dlg = new InputDialog(shell, "Rename Song", "Title:", currentSong.getName(), validator);
            if (dlg.open() == InputDialog.OK) {
                currentSong.setName(dlg.getValue());
                songsCombo.refresh();
            }
        }
    });

    new ToolItem(toolBar, SWT.SEPARATOR);

    play = new ToolItem(toolBar, SWT.PUSH);
    play.setImage(ImageRegistry.getImageFromResources("control_play_blue.png"));
    play.setToolTipText("Play");
    play.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            Runnable playThread = new Runnable() {

                @Override
                public void run() {
                    try {
                        ProjectCompiler compiler = new ProjectCompiler(project);
                        Music music = compiler.build(currentSong);
                        byte[] data = music.toArray();
                        try {
                            serialPort.writeInt('P');
                            serialPort.writeInt(data.length & 0xFF);
                            serialPort.writeInt((data.length >> 8) & 0xFF);
                            serialPort.writeBytes(data);
                        } catch (SerialPortException e) {
                            e.printStackTrace();
                        }
                    } catch (final ProjectException ex) {
                        Display.getDefault().asyncExec(new Runnable() {

                            @Override
                            public void run() {
                                if (shell.isDisposed()) {
                                    return;
                                }
                                MessageDialog.openError(shell, Main.APP_TITLE,
                                        "An error occurred while compiling song:\r\n\r\n" + ex.getMessage());
                                focusOnErrorCell(ex);
                            }
                        });
                    }
                }
            };
            new Thread(playThread).start();
        }
    });

    stop = new ToolItem(toolBar, SWT.PUSH);
    stop.setImage(ImageRegistry.getImageFromResources("control_stop_blue.png"));
    stop.setToolTipText("Stop");
    stop.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            try {
                serialPort.writeInt('0');
            } catch (SerialPortException ex) {
                ex.printStackTrace();
            }
        }
    });

    new ToolItem(toolBar, SWT.SEPARATOR);

    delete = new ToolItem(toolBar, SWT.PUSH);
    delete.setImage(ImageRegistry.getImageFromResources("application_delete.png"));
    delete.setToolTipText("Delete song");
    delete.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            IStructuredSelection selection = songsCombo.getStructuredSelection();
            if (selection.isEmpty()) {
                return;
            }
            if (!MessageDialog.openConfirm(shell, Main.APP_TITLE, "You really want to delete this song?")) {
                return;
            }
            int index = project.getSongs().indexOf(selection.getFirstElement());
            if (index > 0) {
                songsCombo.setSelection(new StructuredSelection(project.getSong(index - 1)));
            } else {
                songsCombo.setSelection(new StructuredSelection(project.getSong(index + 1)));
            }
            project.getObservableSongs().remove(selection.getFirstElement());
            delete.setEnabled(currentSong != null && project.getSongs().size() > 1);
        }
    });

    label = new Label(composite, SWT.NONE);
    label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    label = new Label(composite, SWT.NONE);
    label.setText("BPM");

    bpm = new Spinner(composite, SWT.BORDER | SWT.NO_FOCUS);
    bpm.setValues(120, 0, 9999, 0, 1, 1);
    bpm.setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 5), SWT.DEFAULT));
    bpm.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (currentSong != null) {
                currentSong.setBpm(bpm.getSelection());
            }
        }
    });

    label = new Label(composite, SWT.NONE);
    label.setText("Rows");

    rows = new Spinner(composite, SWT.BORDER | SWT.NO_FOCUS);
    rows.setValues(0, 0, 9999, 0, 1, 1);
    rows.setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 5), SWT.DEFAULT));
    rows.setEnabled(false);
    rows.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            if (currentSong == null) {
                return;
            }
            int totalRows = rows.getSelection();
            while (currentSong.getObservableRows().size() > totalRows) {
                currentSong.getObservableRows().remove(currentSong.getObservableRows().size() - 1);
            }
            while (currentSong.getObservableRows().size() < totalRows) {
                currentSong.getObservableRows().add(new SongRow());
            }
        }
    });

    label = new Label(composite, SWT.NONE);
    label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

    label = new Label(composite, SWT.NONE);
    label.setText("Octave");

    octave = new Spinner(composite, SWT.BORDER | SWT.NO_FOCUS);
    octave.setValues(3, 1, 9, 0, 1, 1);
    octave.setLayoutData(new GridData(Dialog.convertWidthInCharsToPixels(fontMetrics, 3), SWT.DEFAULT));
    octave.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            viewer.getControl().setFocus();
        }
    });

    instrumentToolBar = new InstrumentToolBar(composite);

    songsCombo.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            currentSong = (Song) selection.getFirstElement();
            updateSongView();
        }
    });
}

From source file:com.maccasoft.composer.MusicEditor.java

License:Open Source License

void createMusicViewer(Composite parent) {
    Group group = new Group(parent, SWT.NONE);
    group.setText("Pattern");
    group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    group.setLayout(new GridLayout(1, false));

    final Font font;
    if ("win32".equals(SWT.getPlatform())) {
        font = new Font(Display.getDefault(), "Courier New", 10, SWT.NONE);
    } else {/*from w  w w . ja v a2s.c  o  m*/
        font = new Font(Display.getDefault(), "mono", 10, SWT.NONE);
    }

    GC gc = new GC(group);
    gc.setFont(font);
    FontMetrics fontMetrics = gc.getFontMetrics();
    gc.dispose();

    Grid table = new Grid(group, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    table.setHeaderVisible(true);
    table.setLinesVisible(false);
    table.setRowHeaderVisible(true);
    table.setItemHeaderWidth(Dialog.convertWidthInCharsToPixels(fontMetrics, 6));
    table.setCellSelectionEnabled(true);
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    table.setFont(font);

    table.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            font.dispose();
        }
    });

    viewer = new GridTableViewer(table);

    ObservableListContentProvider contentProvider = new ObservableListContentProvider() {

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
            if (oldInput != null) {
                ((IObservableList) oldInput).removeListChangeListener(listChangeListener);
            }
            if (newInput != null) {
                ((IObservableList) newInput).addListChangeListener(listChangeListener);
            }
            Display.getDefault().timerExec(0, rowBackgroundUpdateRunnable);
            super.inputChanged(viewer, oldInput, newInput);
        }
    };
    viewer.setContentProvider(contentProvider);

    viewer.setRowHeaderLabelProvider(new CellLabelProvider() {

        @Override
        public void update(ViewerCell cell) {
            int index = ((IObservableList) viewer.getInput()).indexOf(cell.getElement());
            cell.setText(String.format("%02X", index));
        }
    });

    for (int ch = 0; ch < Project.channelLabels.length; ch++) {
        final int channel = ch;

        GridColumnGroup columnGroup = new GridColumnGroup(table, SWT.NONE);
        columnGroup.setText(Project.channelLabels[ch]);

        GridColumn column = new GridColumn(columnGroup, SWT.CENTER);
        column.setText("Note");
        column.setWidth(Dialog.convertWidthInCharsToPixels(fontMetrics, 6));
        GridViewerColumn viewerColumn = new GridViewerColumn(viewer, column);
        viewerColumn.setLabelProvider(new CellLabelProvider() {

            @Override
            public void update(ViewerCell cell) {
                SongRow element = (SongRow) cell.getElement();
                cell.setText(element.getNote(channel));
            }
        });
        viewerColumn.setEditingSupport(new TextEditingSupport(viewer) {

            @Override
            protected Object getValue(Object element) {
                return ((SongRow) element).getNote(channel);
            }

            @Override
            protected void setValue(Object element, Object value) {
                ((SongRow) element).setNote(channel, value.toString());
                viewer.update(element, null);
            }
        });

        column = new GridColumn(columnGroup, SWT.CENTER);
        column.setText("Ins.");
        column.setWidth(Dialog.convertWidthInCharsToPixels(fontMetrics, 6));
        viewerColumn = new GridViewerColumn(viewer, column);
        viewerColumn.setLabelProvider(new CellLabelProvider() {

            @Override
            public void update(ViewerCell cell) {
                SongRow element = (SongRow) cell.getElement();
                String value = element.getInstrument(channel);
                cell.setText(value);
                if (project.getInstrument(value) == null) {
                    cell.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
                } else {
                    cell.setForeground(null);
                }
            }
        });
        viewerColumn.setEditingSupport(new TextEditingSupport(viewer) {

            @Override
            protected Object getValue(Object element) {
                return ((SongRow) element).getInstrument(channel);
            }

            @Override
            protected void setValue(Object element, Object value) {
                ((SongRow) element).setInstrument(channel, value.toString());
                viewer.update(element, null);
            }
        });

        column = new GridColumn(columnGroup, SWT.CENTER);
        column.setText("Fx1");
        column.setWidth(Dialog.convertWidthInCharsToPixels(fontMetrics, 6));
        viewerColumn = new GridViewerColumn(viewer, column);
        viewerColumn.setLabelProvider(new CellLabelProvider() {

            @Override
            public void update(ViewerCell cell) {
                SongRow element = (SongRow) cell.getElement();
                String value = element.getFx1(channel);
                cell.setText(value);
                if (value.startsWith("TR") && project.getInstrument(element.getInstrument(channel)) == null) {
                    cell.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
                } else {
                    cell.setForeground(null);
                }
            }
        });
        viewerColumn.setEditingSupport(new TextEditingSupport(viewer) {

            @Override
            protected Object getValue(Object element) {
                return ((SongRow) element).getFx1(channel);
            }

            @Override
            protected void setValue(Object element, Object value) {
                ((SongRow) element).setFx1(channel, value.toString());
                viewer.update(element, null);
            }
        });

        column = new GridColumn(columnGroup, SWT.CENTER);
        column.setText("Fx2");
        column.setWidth(Dialog.convertWidthInCharsToPixels(fontMetrics, 6));
        viewerColumn = new GridViewerColumn(viewer, column);
        viewerColumn.setLabelProvider(new CellLabelProvider() {

            @Override
            public void update(ViewerCell cell) {
                SongRow element = (SongRow) cell.getElement();
                String value = element.getFx2(channel);
                cell.setText(value);
                if (value.startsWith("TR") && project.getInstrument(element.getInstrument(channel)) == null) {
                    cell.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
                } else {
                    cell.setForeground(null);
                }
            }
        });
        viewerColumn.setEditingSupport(new TextEditingSupport(viewer) {

            @Override
            protected Object getValue(Object element) {
                return ((SongRow) element).getFx2(channel);
            }

            @Override
            protected void setValue(Object element, Object value) {
                ((SongRow) element).setFx2(channel, value.toString());
                viewer.update(element, null);
            }
        });
    }

    table.addKeyListener(new NoteKeyListener(viewer) {

        @Override
        protected int getOctave() {
            return octave.getSelection();
        }

        @Override
        protected String getInstrument() {
            IStructuredSelection selection = instrumentToolBar.getStructuredSelection();
            String id = project.getInstrumentId((Instrument) selection.getFirstElement());
            return id != null ? id : super.getInstrument();
        }
    });

    ColumnViewerEditorActivationStrategy activationStrategy = new ColumnViewerEditorActivationStrategy(viewer) {

        @Override
        protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
            return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
                    || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
                    || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
                            && event.character == SWT.CR)
                    || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
        }
    };
    activationStrategy.setEnableEditorActivationWithKeyboard(true);

    GridViewerEditor.create(viewer, activationStrategy, ColumnViewerEditor.TABBING_HORIZONTAL
            | ColumnViewerEditor.KEYBOARD_ACTIVATION | GridViewerEditor.SELECTION_FOLLOWS_EDITOR);
}

From source file:com.nokia.tools.variant.compare.ui.actions.ImportDataAction.java

License:Open Source License

public void run(IAction action) {

    final ImportDataWizard wizard;

    if (modelPart == null) {
        // no CPF is open
        wizard = new ImportDataWizard();
    } else {/*from   w  ww  . java  2s . c o  m*/
        // Opened CPF is set as target
        wizard = new ImportDataWizard(modelPart);
        wizard.setTargetModel((ViewEditorModel) modelPart.getAdapter(ViewEditorModel.class));
    }

    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    WizardDialog dialog = new WizardDialog(window.getShell(), wizard) {
        @Override
        protected void configureShell(Shell newShell) {
            super.configureShell(newShell);
            Image image = ExtendedImageRegistry.INSTANCE
                    .getImage(ImportDataUIPlugin.INSTANCE.getImage("copyvalues_16_tlb.png"));
            newShell.setImage(image);
        }
    };
    dialog.addPageChangingListener(new IPageChangingListener() {
        public void handlePageChanging(PageChangingEvent event) {
            if (event.getCurrentPage() == wizard.getPage(ImportDataWizardPageOne.PAGE_ID)) {
                try {
                    if (!wizard.isCpfOpened()) {
                        wizard.loadSourceAndTargetModel();
                    } else {
                        wizard.loadSourceModel();
                    }
                    event.doit = true;
                } catch (Exception ex) {
                    event.doit = false;
                    ((WizardPage) event.getCurrentPage()).setErrorMessage(ex.toString());
                }
            }
        }
    });

    GC gc = new GC(window.getShell());
    FontMetrics fm = gc.getFontMetrics();
    gc.dispose();
    int minWidth = Dialog.convertWidthInCharsToPixels(fm, 35);
    int minHeight = Dialog.convertHeightInCharsToPixels(fm, 25);
    dialog.setMinimumPageSize(minWidth, minHeight);
    dialog.open();

}