Example usage for org.eclipse.jface.viewers LabelProvider LabelProvider

List of usage examples for org.eclipse.jface.viewers LabelProvider LabelProvider

Introduction

In this page you can find the example usage for org.eclipse.jface.viewers LabelProvider LabelProvider.

Prototype

public LabelProvider() 

Source Link

Document

Creates a new label provider.

Usage

From source file:com.aptana.preview.ui.properties.PreviewSettingComposite.java

License:Open Source License

private void createNewServer() {
    ListDialog dialog = new ListDialog(getShell());
    dialog.setContentProvider(ArrayContentProvider.getInstance());
    dialog.setLabelProvider(new LabelProvider() {

        @Override//w w  w .j  a  va 2  s .c  o m
        public String getText(Object element) {
            if (element instanceof IServerType) {
                return ((IServerType) element).getName();
            }
            return super.getText(element);
        }
    });
    dialog.setInput(WebServerCorePlugin.getDefault().getServerManager().getServerTypes());
    dialog.setTitle(Messages.ProjectPreviewPropertyPage_ChooseServerType);

    Object[] result;
    if (dialog.open() == Window.OK && (result = dialog.getResult()) != null && result.length == 1) {
        String typeId = ((IServerType) result[0]).getId();
        try {
            IServer newConfiguration = WebServerCorePlugin.getDefault().getServerManager().createServer(typeId);
            if (newConfiguration != null) {
                if (editServerConfiguration(newConfiguration)) {
                    WebServerCorePlugin.getDefault().getServerManager().add(newConfiguration);
                    updateServersContent(false);
                    fServersCombo.setSelection(new StructuredSelection(newConfiguration));
                    // forces an update of widget enablements
                    updateStates();
                }
            }
        } catch (CoreException e) {
            PreviewPlugin.log(Messages.ProjectPreviewPropertyPage_ERR_FailToCreateServer, e);
        }
    }
}

From source file:com.aptana.projects.wizards.ProjectTemplateSelectionPage.java

License:Open Source License

private Composite createTemplatesList(Composite parent) {
    Composite main = new Composite(parent, SWT.NONE);
    main.setLayout(GridLayoutFactory.fillDefaults().spacing(0, 0).numColumns(2).create());
    Color background = main.getDisplay().getSystemColor(SWT.COLOR_WHITE);
    main.setBackground(background);/*  w w  w  . j a v a 2  s  .co  m*/

    // the left side is the list of template tags
    Composite templateTags = new Composite(main, SWT.BORDER);
    templateTags.setLayout(GridLayoutFactory.swtDefaults().create());
    // If there are only the "All" tag and one other tag, don't both showing the left column.
    boolean exclude = templateTagsMap.size() <= 2;
    templateTags.setLayoutData(
            GridDataFactory.fillDefaults().grab(false, true).hint(150, SWT.DEFAULT).exclude(exclude).create());
    templateTags.setBackground(background);

    List<String> tags = new ArrayList<String>(templateTagsMap.keySet());
    Collections.sort(tags);
    tagsListViewer = new TableViewer(templateTags, SWT.SINGLE | SWT.FULL_SELECTION);
    tagsListViewer.setContentProvider(ArrayContentProvider.getInstance());
    tagsListViewer.setLabelProvider(new LabelProvider() {

        @Override
        public Image getImage(Object element) {
            if (element instanceof String) {
                return getProjectTemplatesManager().getImageForTag((String) element);
            }
            return super.getImage(element);
        }
    });
    tagsListViewer.setInput(tags);
    tagsListViewer.setComparator(new ViewerComparator() {

        @Override
        public int category(Object element) {
            // make sure the "All" tag appears at the bottom
            return TAG_ALL.equals(element) ? 1 : 0;
        }
    });
    Table tagsList = tagsListViewer.getTable();
    tagsList.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
    tagsList.setBackground(background);
    FontData[] fontData = SWTUtils.resizeFont(tagsList.getFont(), 2);
    final Font tagFont = new Font(tagsList.getDisplay(), fontData);
    tagsList.setFont(tagFont);
    tagsList.addDisposeListener(new DisposeListener() {

        public void widgetDisposed(DisposeEvent e) {
            tagFont.dispose();
        }
    });
    tagsListViewer.addSelectionChangedListener(tagSelectionChangedListener);

    // the right side has the list of templates for the selected tag and the details on the selected template
    Composite rightComp = new Composite(main, SWT.BORDER);
    rightComp.setLayout(GridLayoutFactory.fillDefaults().create());
    rightComp.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
    rightComp.setBackground(background);

    templatesListComposite = new Composite(rightComp, SWT.NONE);
    templatesListComposite.setLayout(
            RowLayoutFactory.swtDefaults().extendedMargins(5, 5, 5, 5).spacing(10).fill(true).create());
    templatesListComposite
            .setLayoutData(GridDataFactory.fillDefaults().grab(true, true).hint(450, 250).create());
    templatesListComposite.setBackground(background);

    Label separator = new Label(rightComp, SWT.SEPARATOR | SWT.HORIZONTAL);
    separator.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).create());

    Composite descriptionComp = createTemplateDescription(rightComp);
    descriptionComp
            .setLayoutData(GridDataFactory.fillDefaults().grab(true, false).hint(SWT.DEFAULT, 110).create());

    return main;
}

From source file:com.aptana.ui.dialogs.InputMessageDialog.java

License:Open Source License

private void createInput(Composite composite) {
    new Label(composite, SWT.NONE);
    Composite parent = new Composite(composite, SWT.NONE);
    parent.setLayout(GridLayoutFactory.fillDefaults().margins(0, 0).numColumns(2).create());
    Label label = new Label(parent, SWT.NONE);
    label.setText(dialogMessage);/*from ww w.jav a2 s.c o  m*/
    GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, false).hint(SWT.DEFAULT, 25)
            .applyTo(label);

    Composite valueComp = new Composite(parent, SWT.NONE);
    valueComp.setLayout(new FillLayout(SWT.HORIZONTAL));

    if (INPUT.equals(inputType) || PASSWORD.equals(inputType)) {
        int flags = SWT.BORDER;
        if (PASSWORD.equals(inputType)) {
            flags |= SWT.PASSWORD;
        }
        Text t = new Text(valueComp, flags);
        input.add(t);
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false)
                .hint(convertHorizontalDLUsToPixels(160), 25).applyTo(valueComp);
    } else if (CHECKBOX.equals(inputType)) {
        for (JsonNode value : values) {
            Button b = new Button(valueComp, SWT.CHECK);
            b.setText(value.asText());
            input.add(b);
        }
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(valueComp);
    } else if (LIST.equals(inputType)) {
        Combo l = new Combo(valueComp, SWT.NONE);
        ComboViewer combo = new ComboViewer(l);
        input.add(combo);
        combo.setContentProvider(new ArrayContentProvider() {
            @Override
            public Object[] getElements(Object inputElement) {
                if (inputElement instanceof ArrayNode) {
                    ArrayNode arrayNode = (ArrayNode) inputElement;
                    JsonNode[] names = new JsonNode[arrayNode.size()];
                    int i = 0;
                    for (JsonNode node : arrayNode) {
                        names[i++] = node;
                    }
                    return names;
                }
                return super.getElements(inputElement);
            }
        });
        combo.setInput(values);

        combo.setLabelProvider(new LabelProvider() {
            @Override
            public String getText(Object element) {
                if (element instanceof ObjectNode) {
                    String name = ((ObjectNode) element).path(NAME).asText();
                    if (StringUtil.isEmpty(name)) {
                        return ((ObjectNode) element).asText();
                    }
                    return name;
                } else if (element instanceof JsonNode) {
                    return ((JsonNode) element).asText();
                }
                return element.toString();
            }
        });
        combo.setSelection(new StructuredSelection(values.get(0)));
        GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(valueComp);
    }
}

From source file:com.aptana.ui.dialogs.MultipleInputMessageDialog.java

License:Open Source License

private void createInput(Composite composite) {
    for (JsonNode question : questionsNode) {
        List<Object> input = new ArrayList<Object>(1);

        new Label(composite, SWT.NONE);
        Composite parent = new Composite(composite, SWT.NONE);
        parent.setLayout(/*ww w .ja  va  2  s  . c om*/
                GridLayoutFactory.fillDefaults().margins(0, 0).numColumns(2).equalWidth(false).create());
        Label label = new Label(parent, SWT.NONE);
        String lblTxt = question.path(MESSAGE).asText();
        label.setText(lblTxt);
        Point requiredSize = label.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        int minSize = convertHorizontalDLUsToPixels(80);
        if (requiredSize.x > minSize) {
            minSize = requiredSize.x;
        }
        GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, false)
                .hint(minSize, SWT.DEFAULT).applyTo(label);

        Composite valueComp = new Composite(parent, SWT.NONE);
        valueComp.setLayout(new FillLayout(SWT.HORIZONTAL));
        JsonNode choices = question.path(CHOICES);
        ArrayNode values = JsonNodeFactory.instance.arrayNode();
        if (!choices.isArray()) {
            values.add(choices);
        } else {
            values = (ArrayNode) choices;
        }
        String inputType = question.path(TYPE).asText();
        String responseKey = question.path(NAME).asText();

        if (INPUT.equals(inputType) || PASSWORD.equals(inputType)) {
            int flags = SWT.BORDER;
            if (PASSWORD.equals(inputType)) {
                flags |= SWT.PASSWORD;
            }
            Text t = new Text(valueComp, flags);
            input.add(t);

            GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false)
                    .hint(convertHorizontalDLUsToPixels(160), 25).applyTo(valueComp);
        } else if (CHECKBOX.equals(inputType)) {
            for (JsonNode value : values) {
                Button b = new Button(valueComp, SWT.CHECK);
                b.setText(value.asText());
                input.add(b);
            }
            GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(valueComp);
        } else if (LIST.equals(inputType)) {
            Combo l = new Combo(valueComp, SWT.READ_ONLY);
            ComboViewer combo = new ComboViewer(l);
            input.add(combo);
            combo.setContentProvider(new ArrayContentProvider() {
                @Override
                public Object[] getElements(Object inputElement) {
                    if (inputElement instanceof ArrayNode) {
                        ArrayNode arrayNode = (ArrayNode) inputElement;
                        JsonNode[] names = new JsonNode[arrayNode.size()];
                        int i = 0;
                        for (JsonNode node : arrayNode) {
                            names[i++] = node;
                        }
                        return names;
                    }
                    return super.getElements(inputElement);
                }
            });
            combo.setInput(values);
            combo.setLabelProvider(new LabelProvider() {
                @Override
                public String getText(Object element) {
                    if (element instanceof ObjectNode) {
                        String name = ((ObjectNode) element).path(NAME).asText();
                        if (StringUtil.isEmpty(name)) {
                            return ((ObjectNode) element).asText();
                        }
                        return name;
                    } else if (element instanceof JsonNode) {
                        return ((JsonNode) element).asText();
                    }
                    return element.toString();
                }
            });
            JsonNode defaultValue = question.path(DEFAULT);
            if (defaultValue != null && values instanceof ArrayNode) {
                ArrayNode arrayNode = (ArrayNode) values;
                for (JsonNode node : arrayNode) {
                    if (ObjectUtil.areEqual(node.get(VALUE).textValue(), defaultValue.textValue())) {
                        combo.setSelection(new StructuredSelection(node));
                        break;
                    }
                }
            } else {
                combo.setSelection(new StructuredSelection(values.get(0)));
            }
            GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(valueComp);
        } else if (CONFIRMATION.equals(inputType)) {
            input.add(Boolean.TRUE);
            setButtonLabels(new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL });//TISTUD-7408
        }

        userInput.putPOJO(responseKey, input);

    }
}

From source file:com.aptana.ui.widgets.CListTable.java

License:Open Source License

private void createTable(Composite parent) {
    tableViewer = new TableViewer(parent, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
    tableViewer.getControl().setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());

    tableViewer.setContentProvider(ArrayContentProvider.getInstance());
    tableViewer.setLabelProvider(new LabelProvider());
    tableViewer.setComparator(new ViewerComparator());
    tableViewer.setInput(items);/* ww  w  .  ja  v a 2s.  c  om*/
    tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            updateStates();
        }

    });
    updateStates();
}

From source file:com.aptana.webserver.ui.internal.actions.AddServerHandler.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {
    ListDialog dlg = new ListDialog(UIUtils.getActiveShell());
    dlg.setContentProvider(ArrayContentProvider.getInstance());
    dlg.setLabelProvider(new LabelProvider() {
        @Override//w ww.  ja va2  s. c  om
        public Image getImage(Object element) {

            if (element instanceof Identifiable) {
                Identifiable identifiable = (Identifiable) element;
                String id = identifiable.getId();
                ImageRegistry imageRegistry = WebServerUIPlugin.getDefault().getImageRegistry();
                Image image = imageRegistry.get(id);
                if (image != null) {
                    return image;
                }
                ImageDescriptor desc = ImageAssociations.getInstance().getImageDescriptor(id);
                if (desc != null) {
                    imageRegistry.put(id, desc);
                    return imageRegistry.get(id);
                }
            }
            return WebServerUIPlugin.getImage(WebServerUIPlugin.SERVER_ICON);
        }

        @Override
        public String getText(Object element) {
            if (element instanceof IServerType) {
                return ((IServerType) element).getName();
            }
            return super.getText(element);
        }
    });
    dlg.setInput(WebServerCorePlugin.getDefault().getServerManager().getServerTypes());
    dlg.setTitle(Messages.ServersPreferencePage_Title);
    if (dlg.open() != Window.OK) {
        return null;
    }
    Object[] result = dlg.getResult();
    if (result != null && result.length == 1) {
        String typeId = ((IServerType) result[0]).getId();
        createServer(typeId);
    }
    return null;
}

From source file:com.aptana.webserver.ui.preferences.ServersPreferencePage.java

License:Open Source License

@Override
protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setFont(parent.getFont());
    composite.setLayout(GridLayoutFactory.swtDefaults().numColumns(2).create());

    viewer = new ListViewer(composite, SWT.SINGLE | SWT.BORDER);
    viewer.getControl().setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
    viewer.setContentProvider(new ArrayContentProvider() {
        @Override/*from   w w  w  . j a v a 2  s .c o m*/
        public Object[] getElements(Object inputElement) {
            if (inputElement instanceof IServerManager) {
                inputElement = ((IServerManager) inputElement).getServers(); // $codepro.audit.disable
                // questionableAssignment
            }
            return super.getElements(inputElement);
        }

    });
    viewer.setLabelProvider(new LabelProvider() {
        @Override
        public Image getImage(Object element) {
            return null; // TODO: use ImageAssociations
        }

        @Override
        public String getText(Object element) {
            if (element instanceof IServer) {
                return ((IServer) element).getName();
            }
            return super.getText(element);
        }

    });
    viewer.setInput(WebServerCorePlugin.getDefault().getServerManager());

    Composite buttonContainer = new Composite(composite, SWT.NONE);
    buttonContainer.setLayoutData(GridDataFactory.fillDefaults().grab(false, true).create());
    buttonContainer.setLayout(GridLayoutFactory.swtDefaults().create());

    Button newButton = new Button(buttonContainer, SWT.PUSH);
    newButton.setText(StringUtil.ellipsify(CoreStrings.NEW));
    newButton
            .setLayoutData(GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER)
                    .hint(Math.max(newButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x,
                            convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH)), SWT.DEFAULT)
                    .create());

    final Button editButton = new Button(buttonContainer, SWT.PUSH);
    editButton.setText(StringUtil.ellipsify(CoreStrings.EDIT));
    editButton.setLayoutData(GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).create());

    final Button deleteButton = new Button(buttonContainer, SWT.PUSH);
    deleteButton.setText(CoreStrings.DELETE);
    deleteButton.setLayoutData(GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).create());

    newButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            ListDialog dlg = new ListDialog(getShell());
            dlg.setContentProvider(ArrayContentProvider.getInstance());
            dlg.setLabelProvider(new LabelProvider() {
                @Override
                public Image getImage(Object element) {
                    return null; // TODO: use ImageAssociations
                }

                @Override
                public String getText(Object element) {
                    if (element instanceof IServerType) {
                        return ((IServerType) element).getName();
                    }
                    return super.getText(element);
                }
            });
            dlg.setInput(WebServerCorePlugin.getDefault().getServerManager().getServerTypes());
            dlg.setTitle(Messages.ServersPreferencePage_Title);
            Object[] result;
            if (dlg.open() == Window.OK && (result = dlg.getResult()) != null && result.length == 1) { // $codepro.audit.disable assignmentInCondition
                String typeId = ((IServerType) result[0]).getId();
                try {
                    IServer newConfiguration = WebServerCorePlugin.getDefault().getServerManager()
                            .createServer(typeId);
                    if (newConfiguration != null) {
                        if (editServerConfiguration(newConfiguration)) {
                            WebServerCorePlugin.getDefault().getServerManager().add(newConfiguration);
                            viewer.refresh();
                        }
                    }
                } catch (CoreException e) {
                    IdeLog.logError(WebServerUIPlugin.getDefault(), e);
                }
            }
        }

    });
    editButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            IServer selection = (IServer) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
            if (selection != null && editServerConfiguration(selection)) {
                viewer.refresh();
            }
        }

    });
    deleteButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            IServer selection = (IServer) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
            if (selection != null
                    && MessageDialog.openQuestion(getShell(), Messages.ServersPreferencePage_DeletePrompt_Title,
                            Messages.ServersPreferencePage_DeletePrompt_Message)) {
                WebServerCorePlugin.getDefault().getServerManager().remove(selection);
                viewer.refresh();
            }
        }

    });

    viewer.addDoubleClickListener(new IDoubleClickListener() {
        public void doubleClick(DoubleClickEvent event) {
            IServer selection = (IServer) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
            if (selection != null && editServerConfiguration(selection)) {
                viewer.refresh();
            }
        }
    });
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
            boolean hasSelection = !event.getSelection().isEmpty();
            editButton.setEnabled(hasSelection);
            deleteButton.setEnabled(hasSelection);
        }
    });
    viewer.setSelection(StructuredSelection.EMPTY);

    return composite;
}

From source file:com.arc.cdt.importer.internal.ui.ProjectSelectionWizardPage.java

License:Open Source License

public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NULL);
    composite.setLayout(new GridLayout(2, false));
    mViewer = CheckboxTableViewer.newCheckList(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    GridData gridData = new GridData(GridData.FILL_BOTH);
    gridData.verticalSpan = 3;/*w ww.ja  va 2 s  .c o  m*/
    mViewer.getControl().setLayoutData(gridData);

    mViewer.setContentProvider(new IStructuredContentProvider() {

        public Object[] getElements(Object inputElement) {
            if (inputElement instanceof ICodewrightProjectSpace) {
                try {
                    return ((ICodewrightProjectSpace) inputElement).getProjects();
                } catch (Exception e) {
                    return new Exception[] { e };
                }
            } else
                return EMPTY;
        }

        public void dispose() {
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }

    });

    mViewer.setLabelProvider(new LabelProvider() {
        @Override
        public String getText(Object element) {
            if (element instanceof ICodewrightProject) {
                return ((ICodewrightProject) element).getName();
            } else if (element instanceof Exception) {
                return ((Exception) element).getMessage();
            } else
                return "";
        }

        @Override
        public Image getImage(Object element) {
            if (element instanceof ICodewrightProject) {
                return ImporterPlugin.getDefault().getImageRegistry().get(PROJECT_IMAGE_KEY);
            }
            return null;
        }

    });

    mViewer.addCheckStateListener(new ICheckStateListener() {

        public void checkStateChanged(CheckStateChangedEvent event) {
            validate();
        }
    });

    Button setAllButton = new Button(composite, SWT.PUSH);
    setAllButton.setText("Select All");
    setAllButton.setLayoutData(new GridData());
    setAllButton.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent e) {
            mViewer.setAllChecked(true);
            validate();
        }

        public void widgetDefaultSelected(SelectionEvent e) {
            widgetSelected(e);

        }
    });

    Button clearAllButton = new Button(composite, SWT.PUSH);
    clearAllButton.setText("Clear All");
    clearAllButton.setLayoutData(new GridData());
    clearAllButton.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent e) {
            mViewer.setAllChecked(false);
            validate();
        }

        public void widgetDefaultSelected(SelectionEvent e) {
            widgetSelected(e);

        }
    });

    setControl(composite);

}

From source file:com.arc.embeddedcdt.launch.LaunchShortcut.java

License:Open Source License

/**
 * Method chooseDebugConfig.//from  www .  j a  v a 2s  .c  o m
 * @param debugConfigs
 * @param mode
 * @return ICDebugConfiguration
 */
private ICDebugConfiguration chooseDebugConfig(ICDebugConfiguration[] debugConfigs, String mode) {
    LabelProvider provider = new LabelProvider() {
        /**
         * The <code>LabelProvider</code> implementation of this 
         * <code>ILabelProvider</code> method returns the element's <code>toString</code>
         * string. Subclasses may override.
         */
        public String getText(Object element) {
            if (element == null) {
                return ""; //$NON-NLS-1$
            } else if (element instanceof ICDebugConfiguration) {
                return ((ICDebugConfiguration) element).getName();
            }
            return element.toString();
        }
    };
    ElementListSelectionDialog dialog = new ElementListSelectionDialog(getShell(), provider);
    dialog.setElements(debugConfigs);
    dialog.setTitle(getDebugConfigDialogTitleString(debugConfigs, mode));
    dialog.setMessage(getDebugConfigDialogMessageString(debugConfigs, mode));
    dialog.setMultipleSelection(false);
    int result = dialog.open();
    provider.dispose();
    if (result == Window.OK) {
        return (ICDebugConfiguration) dialog.getFirstResult();
    }
    return null;
}

From source file:com.archimatetool.editor.preferences.ColoursFontsPreferencePage.java

License:Open Source License

private void createColoursTab() {
    // Reset everything
    resetColorsCache(false);//from ww  w  .j a  v a2  s .c o m
    fImageRegistry = new ImageRegistry();

    Composite client = new Composite(fTabfolder, SWT.NULL);
    client.setLayout(new GridLayout(2, false));

    TabItem item = new TabItem(fTabfolder, SWT.NONE);
    item.setText(Messages.ColoursFontsPreferencePage_23);
    item.setControl(client);

    Label label = new Label(client, SWT.NULL);
    label.setText(Messages.ColoursFontsPreferencePage_0);
    GridData gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalSpan = 2;
    label.setLayoutData(gd);

    // Tree
    fTreeViewer = new TreeViewer(client);
    gd = new GridData(GridData.FILL_BOTH);
    gd.heightHint = 80; // need this to set a smaller height
    fTreeViewer.getTree().setLayoutData(gd);

    // Tree Double-click listener
    fTreeViewer.addDoubleClickListener(new IDoubleClickListener() {
        @Override
        public void doubleClick(DoubleClickEvent event) {
            Object[] selected = ((IStructuredSelection) fTreeViewer.getSelection()).toArray();
            if (isValidTreeSelection(selected)) {
                RGB newRGB = openColorDialog(selected[0]);
                if (newRGB != null) {
                    for (Object object : selected) {
                        setColor(object, newRGB);
                    }
                }
            }
        }
    });

    // Tree Selection Changed Listener
    fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            Object[] selected = ((IStructuredSelection) event.getSelection()).toArray();
            fEditFillColorButton.setEnabled(isValidTreeSelection(selected));
            fResetFillColorButton.setEnabled(isValidTreeSelection(selected));
        }
    });

    // Tree Content Provider
    fTreeViewer.setContentProvider(new ITreeContentProvider() {

        @Override
        public void dispose() {
        }

        @Override
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }

        @Override
        public Object[] getElements(Object inputElement) {
            if (inputElement instanceof String) {
                return new Object[] {
                        new TreeGrouping(Messages.ColoursFontsPreferencePage_7,
                                ArchimateModelUtils.getBusinessClasses()),
                        new TreeGrouping(Messages.ColoursFontsPreferencePage_8,
                                ArchimateModelUtils.getApplicationClasses()),
                        new TreeGrouping(Messages.ColoursFontsPreferencePage_9,
                                ArchimateModelUtils.getTechnologyClasses()),
                        new TreeGrouping(Messages.ColoursFontsPreferencePage_10,
                                ArchimateModelUtils.getMotivationClasses()),
                        new TreeGrouping(Messages.ColoursFontsPreferencePage_11,
                                ArchimateModelUtils.getImplementationMigrationClasses()),
                        new TreeGrouping(Messages.ColoursFontsPreferencePage_17,
                                new Object[] { IArchimatePackage.eINSTANCE.getDiagramModelNote(),
                                        IArchimatePackage.eINSTANCE.getDiagramModelGroup() }),
                        DEFAULT_ELEMENT_LINE_COLOR, DEFAULT_CONNECTION_LINE_COLOR };
            }

            return null;
        }

        @Override
        public Object[] getChildren(Object parentElement) {
            if (parentElement instanceof TreeGrouping) {
                return ((TreeGrouping) parentElement).children;
            }

            return null;
        }

        @Override
        public Object getParent(Object element) {
            return null;
        }

        @Override
        public boolean hasChildren(Object element) {
            return element instanceof TreeGrouping;
        }

    });

    // Tree Label Provider
    fTreeViewer.setLabelProvider(new LabelProvider() {
        @Override
        public String getText(Object element) {
            if (element instanceof EClass) {
                return ArchimateLabelProvider.INSTANCE.getDefaultName((EClass) element);
            }
            if (element instanceof TreeGrouping) {
                return ((TreeGrouping) element).title;
            }
            if (element instanceof String) {
                String s = (String) element;
                if (s.equals(DEFAULT_ELEMENT_LINE_COLOR)) {
                    return Messages.ColoursFontsPreferencePage_12;
                }
                if (s.equals(DEFAULT_CONNECTION_LINE_COLOR)) {
                    return Messages.ColoursFontsPreferencePage_18;
                }
            }

            return null;
        }

        @Override
        public Image getImage(Object element) {
            if (element instanceof TreeGrouping) {
                return IArchimateImages.ImageFactory.getImage(IArchimateImages.ECLIPSE_IMAGE_FOLDER);
            }

            return getColorSwatch(element);
        }

        // Create a coloured image based on colour and add to the image registry
        private Image getColorSwatch(Object object) {
            String key = getColorKey(object);
            Image image = fImageRegistry.get(key);
            if (image == null) {
                image = new Image(Display.getCurrent(), 16, 16);
                GC gc = new GC(image);
                SWTGraphics graphics = new SWTGraphics(gc);
                graphics.setBackgroundColor(fColorsCache.get(object));
                graphics.fillRectangle(0, 0, 15, 15);
                graphics.drawRectangle(0, 0, 15, 15);
                gc.dispose();
                graphics.dispose();
                fImageRegistry.put(key, image);
            }

            return image;
        }

    });

    //fTreeViewer.setAutoExpandLevel(2);

    // Set Content in Tree
    fTreeViewer.setInput(""); //$NON-NLS-1$

    // Buttons
    Composite buttonClient = new Composite(client, SWT.NULL);
    gd = new GridData(SWT.TOP, SWT.TOP, false, false);
    buttonClient.setLayoutData(gd);
    buttonClient.setLayout(new GridLayout());

    // Edit...
    fEditFillColorButton = new Button(buttonClient, SWT.PUSH);
    fEditFillColorButton.setText(Messages.ColoursFontsPreferencePage_13);
    fEditFillColorButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    fEditFillColorButton.setEnabled(false);
    fEditFillColorButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            Object[] selected = ((IStructuredSelection) fTreeViewer.getSelection()).toArray();
            if (isValidTreeSelection(selected)) {
                RGB newRGB = openColorDialog(selected[0]);
                if (newRGB != null) {
                    for (Object object : selected) {
                        setColor(object, newRGB);
                    }
                }
            }
        }
    });

    // Reset
    fResetFillColorButton = new Button(buttonClient, SWT.PUSH);
    fResetFillColorButton.setText(Messages.ColoursFontsPreferencePage_14);
    fResetFillColorButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    fResetFillColorButton.setEnabled(false);
    fResetFillColorButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            Object[] selected = ((IStructuredSelection) fTreeViewer.getSelection()).toArray();
            if (isValidTreeSelection(selected)) {
                for (Object object : selected) {
                    resetColorToInbuiltDefault(object);
                }
            }
        }
    });

    // Import Scheme
    Button importButton = new Button(buttonClient, SWT.PUSH);
    importButton.setText(Messages.ColoursFontsPreferencePage_2);
    importButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    importButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            try {
                importUserColors();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });

    // Export Scheme
    Button exportButton = new Button(buttonClient, SWT.PUSH);
    exportButton.setText(Messages.ColoursFontsPreferencePage_3);
    exportButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    exportButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            try {
                exportUserColors();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });

    Group elementColorGroup = new Group(client, SWT.NULL);
    elementColorGroup.setLayout(new GridLayout(2, false));
    elementColorGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    elementColorGroup.setText(Messages.ColoursFontsPreferencePage_20);

    // Derive element line colours
    fDeriveElementLineColorsButton = new Button(elementColorGroup, SWT.CHECK);
    fDeriveElementLineColorsButton.setText(Messages.ColoursFontsPreferencePage_19);
    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalSpan = 2;
    fDeriveElementLineColorsButton.setLayoutData(gd);
    fDeriveElementLineColorsButton.setSelection(getPreferenceStore().getBoolean(DERIVE_ELEMENT_LINE_COLOR));
    fDeriveElementLineColorsButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            fElementLineColorContrastSpinner.setEnabled(fDeriveElementLineColorsButton.getSelection());
            fContrastFactorLabel.setEnabled(fDeriveElementLineColorsButton.getSelection());
        }
    });

    fContrastFactorLabel = new Label(elementColorGroup, SWT.NULL);
    fContrastFactorLabel.setText(Messages.ColoursFontsPreferencePage_21);

    fElementLineColorContrastSpinner = new Spinner(elementColorGroup, SWT.BORDER);
    fElementLineColorContrastSpinner.setMinimum(1);
    fElementLineColorContrastSpinner.setMaximum(10);
    fElementLineColorContrastSpinner
            .setSelection(getPreferenceStore().getInt(DERIVE_ELEMENT_LINE_COLOR_FACTOR));

    label = new Label(elementColorGroup, SWT.NULL);
    label.setText(Messages.ColoursFontsPreferencePage_22);

    gd = new GridData(GridData.FILL_HORIZONTAL);
    gd.horizontalSpan = 2;

    // Persist user default colours
    fPersistUserDefaultColors = new Button(client, SWT.CHECK);
    fPersistUserDefaultColors.setText(Messages.ColoursFontsPreferencePage_1);
    fPersistUserDefaultColors.setLayoutData(gd);
    fPersistUserDefaultColors.setSelection(getPreferenceStore().getBoolean(SAVE_USER_DEFAULT_COLOR));

    // Use colours in application
    fShowUserDefaultFillColorsInApplication = new Button(client, SWT.CHECK);
    fShowUserDefaultFillColorsInApplication.setText(Messages.ColoursFontsPreferencePage_6);
    fShowUserDefaultFillColorsInApplication.setLayoutData(gd);
    fShowUserDefaultFillColorsInApplication
            .setSelection(getPreferenceStore().getBoolean(SHOW_FILL_COLORS_IN_GUI));
}