Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2012-06-07 10:14:51 +0000
committerEike Stepper2012-06-07 10:14:51 +0000
commita25531ae8a5c85586fdb590c0f806be3a807b634 (patch)
tree7849f8065be919611dabd87403f9bf5a1743cee7 /plugins/org.eclipse.net4j.util.ui
parentb48ccc46da60cf75aef219ae635c627fb49d3cf2 (diff)
downloadcdo-a25531ae8a5c85586fdb590c0f806be3a807b634.tar.gz
cdo-a25531ae8a5c85586fdb590c0f806be3a807b634.tar.xz
cdo-a25531ae8a5c85586fdb590c0f806be3a807b634.zip
[381472] Design a repository administration API
https://bugs.eclipse.org/bugs/show_bug.cgi?id=381472
Diffstat (limited to 'plugins/org.eclipse.net4j.util.ui')
-rw-r--r--plugins/org.eclipse.net4j.util.ui/Snippet1.java121
-rw-r--r--plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/internal/ui/messages/messages.properties117
-rw-r--r--plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/internal/ui/views/Net4jIntrospectorView.java1572
-rw-r--r--plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/ui/FactoryTypeContentProvider.java55
-rw-r--r--plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/ui/container/ElementWizardAction.java3
-rw-r--r--plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/ui/views/ContainerItemProvider.java1428
6 files changed, 1652 insertions, 1644 deletions
diff --git a/plugins/org.eclipse.net4j.util.ui/Snippet1.java b/plugins/org.eclipse.net4j.util.ui/Snippet1.java
deleted file mode 100644
index 954296a05b..0000000000
--- a/plugins/org.eclipse.net4j.util.ui/Snippet1.java
+++ /dev/null
@@ -1,121 +0,0 @@
-/*
- * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Eike Stepper - initial API and implementation
- */
-package org.eclipse.net4j.util.ui.container.examples;
-
-import org.eclipse.net4j.util.container.IPluginContainer;
-import org.eclipse.net4j.util.factory.Factory;
-import org.eclipse.net4j.util.factory.ProductCreationException;
-import org.eclipse.net4j.util.ui.UIUtil;
-import org.eclipse.net4j.util.ui.container.ElementWizard;
-import org.eclipse.net4j.util.ui.container.ElementWizardComposite;
-import org.eclipse.net4j.util.ui.container.ElementWizardFactory;
-import org.eclipse.net4j.util.ui.container.IElementWizard;
-
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.swt.widgets.Label;
-import org.eclipse.swt.widgets.Shell;
-import org.eclipse.swt.widgets.Text;
-
-/**
- * @author Eike Stepper
- */
-public class Snippet1
-{
- public static void main(String[] args)
- {
- IPluginContainer container = IPluginContainer.INSTANCE;
- String PG = "test.connectors";
-
- container.registerFactory(new Factory(PG, "tcp")
- {
- public Object create(String description) throws ProductCreationException
- {
- return null;
- }
- });
-
- container.registerFactory(new Factory(PG, "jvm")
- {
- public Object create(String description) throws ProductCreationException
- {
- return null;
- }
- });
-
- container.registerFactory(new Factory(PG, "http")
- {
- public Object create(String description) throws ProductCreationException
- {
- return null;
- }
- });
-
- container.registerFactory(new ElementWizardFactory(PG, "http")
- {
- @Override
- public IElementWizard create(String description) throws ProductCreationException
- {
- return new ElementWizard()
- {
- @Override
- protected void create(Composite parent)
- {
- addText(parent, "Server:");
- addCombo(parent, "Protocol:", "http", "https");
- addText(parent, "Resource:");
- }
- };
- }
- });
-
- container.registerFactory(new ElementWizardFactory(PG, "tcp")
- {
- @Override
- public IElementWizard create(String description) throws ProductCreationException
- {
- return new ElementWizard()
- {
- @Override
- protected void create(Composite parent)
- {
- Label l1 = new Label(parent, SWT.NONE);
- l1.setText("Hostname:");
- l1.setLayoutData(UIUtil.createGridData(false, false));
-
- Text t1 = new Text(parent, SWT.BORDER);
- t1.setLayoutData(UIUtil.createGridData(true, false));
- }
- };
- }
- });
-
- Display display = new Display();
- Shell shell = new Shell(display);
- shell.setLayout(new GridLayout(1, false));
-
- new ElementWizardComposite.WithCombo(shell, SWT.NONE, PG, "Type:");
-
- shell.pack();
- shell.open();
- while (!shell.isDisposed())
- {
- if (!display.readAndDispatch())
- {
- display.sleep();
- }
- }
-
- display.dispose();
- }
-}
diff --git a/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/internal/ui/messages/messages.properties b/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/internal/ui/messages/messages.properties
index fed153b915..0ceee5398c 100644
--- a/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/internal/ui/messages/messages.properties
+++ b/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/internal/ui/messages/messages.properties
@@ -1,58 +1,59 @@
-# ==============================================================================
-# Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
-# All rights reserved. This program and the accompanying materials
-# are made available under the terms of the Eclipse Public License v1.0
-# which accompanies this distribution, and is available at
-# http://www.eclipse.org/legal/epl-v10.html
-#
-# Contributors:
-# Victor Roldan Betancort - initial API and implementation
-# Eike Stepper - maintenance
-# ==============================================================================
-
-# ==============================================================================
-# This properties file contains all strings subject to be shown in the UI.
-# ==============================================================================
-
-ContainerView_1=Refresh
-ContainerView_2=Refresh view
-CredentialsDialog_1=Enter your user ID and password.
-CredentialsDialog_2=User ID:
-CredentialsDialog_3=Password:
-CredentialsDialog_0=Login
-IntrospectAction_0=Introspect
-MasterDetailsView_2=Refresh
-MasterDetailsView_3=Refresh
-Net4jContainerItemProvider_0=Remove
-Net4jContainerItemProvider.1=Remove
-Net4jIntrospectorView_1=
-Net4jIntrospectorView_10=Type
-Net4jIntrospectorView_11=Element
-Net4jIntrospectorView_12=Type
-Net4jIntrospectorView_13=Index
-Net4jIntrospectorView_14=Element
-Net4jIntrospectorView_15=Type
-Net4jIntrospectorView_16=Back
-Net4jIntrospectorView_17=Container
-Net4jIntrospectorView_18=null
-Net4jIntrospectorView_21=null
-Net4jIntrospectorView_22=
-Net4jIntrospectorView_24=null
-Net4jIntrospectorView_25=
-Net4jIntrospectorView_27=null
-Net4jIntrospectorView_28=null
-Net4jIntrospectorView_29=
-Net4jIntrospectorView_4=Field
-Net4jIntrospectorView_5=Value
-Net4jIntrospectorView_6=Declared Type
-Net4jIntrospectorView_7=Concrete Type
-Net4jIntrospectorView_8=Key
-Net4jIntrospectorView_9=Value
-SafeAction_0=...
-SafeAction_1=See the Error log for details.
-SafeActionDelegate_0=See the Error log for details.
-SafeActionDelegate_1=Error
-SashLayoutAction_0=Vertical
-SashLayoutAction_1=Horizontal
-SashLayoutAction_2=Layout
-TextAndDisable.0=Disabled
+# ==============================================================================
+# Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+#
+# Contributors:
+# Victor Roldan Betancort - initial API and implementation
+# Eike Stepper - maintenance
+# ==============================================================================
+
+# ==============================================================================
+# This properties file contains all strings subject to be shown in the UI.
+# ==============================================================================
+
+ContainerView_1=Refresh
+ContainerView_2=Refresh view
+CredentialsDialog_1=Enter your user ID and password.
+CredentialsDialog_2=User ID:
+CredentialsDialog_3=Password:
+CredentialsDialog_0=Login
+IntrospectAction_0=Introspect
+MasterDetailsView_2=Refresh
+MasterDetailsView_3=Refresh
+Net4jContainerItemProvider_0=Remove
+Net4jContainerItemProvider.1=Remove
+Net4jIntrospectorView_1=
+Net4jIntrospectorView_10=Type
+Net4jIntrospectorView_11=Element
+Net4jIntrospectorView_12=Type
+Net4jIntrospectorView_13=Index
+Net4jIntrospectorView_14=Element
+Net4jIntrospectorView_15=Type
+Net4jIntrospectorView_16=Back
+Net4jIntrospectorView_17=Container
+Net4jIntrospectorView_17b=Workbench Part
+Net4jIntrospectorView_18=null
+Net4jIntrospectorView_21=null
+Net4jIntrospectorView_22=
+Net4jIntrospectorView_24=null
+Net4jIntrospectorView_25=
+Net4jIntrospectorView_27=null
+Net4jIntrospectorView_28=null
+Net4jIntrospectorView_29=
+Net4jIntrospectorView_4=Field
+Net4jIntrospectorView_5=Value
+Net4jIntrospectorView_6=Declared Type
+Net4jIntrospectorView_7=Concrete Type
+Net4jIntrospectorView_8=Key
+Net4jIntrospectorView_9=Value
+SafeAction_0=...
+SafeAction_1=See the Error log for details.
+SafeActionDelegate_0=See the Error log for details.
+SafeActionDelegate_1=Error
+SashLayoutAction_0=Vertical
+SashLayoutAction_1=Horizontal
+SashLayoutAction_2=Layout
+TextAndDisable.0=Disabled
diff --git a/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/internal/ui/views/Net4jIntrospectorView.java b/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/internal/ui/views/Net4jIntrospectorView.java
index c668363df1..38ad6e9c18 100644
--- a/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/internal/ui/views/Net4jIntrospectorView.java
+++ b/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/internal/ui/views/Net4jIntrospectorView.java
@@ -1,743 +1,829 @@
-/*
- * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Eike Stepper - initial API and implementation
- */
-package org.eclipse.net4j.util.internal.ui.views;
-
-import org.eclipse.net4j.ui.shared.SharedIcons;
-import org.eclipse.net4j.util.ReflectUtil;
-import org.eclipse.net4j.util.WrappedException;
-import org.eclipse.net4j.util.collection.Pair;
-import org.eclipse.net4j.util.container.IPluginContainer;
-import org.eclipse.net4j.util.event.EventUtil;
-import org.eclipse.net4j.util.event.IEvent;
-import org.eclipse.net4j.util.event.IListener;
-import org.eclipse.net4j.util.internal.ui.bundle.OM;
-import org.eclipse.net4j.util.internal.ui.messages.Messages;
-import org.eclipse.net4j.util.ui.UIUtil;
-
-import org.eclipse.jface.action.Action;
-import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.action.IMenuManager;
-import org.eclipse.jface.action.IToolBarManager;
-import org.eclipse.jface.viewers.DoubleClickEvent;
-import org.eclipse.jface.viewers.IDoubleClickListener;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.IStructuredContentProvider;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.jface.viewers.ITableLabelProvider;
-import org.eclipse.jface.viewers.LabelProvider;
-import org.eclipse.jface.viewers.TableViewer;
-import org.eclipse.jface.viewers.Viewer;
-import org.eclipse.jface.viewers.ViewerSorter;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.custom.StackLayout;
-import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.graphics.Image;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Table;
-import org.eclipse.swt.widgets.TableColumn;
-import org.eclipse.swt.widgets.Text;
-import org.eclipse.ui.IActionBars;
-import org.eclipse.ui.ISelectionListener;
-import org.eclipse.ui.ISharedImages;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.IWorkbenchPart;
-import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.part.ViewPart;
-
-import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Stack;
-
-/**
- * @author Eike Stepper
- */
-public class Net4jIntrospectorView extends ViewPart implements ISelectionListener, IDoubleClickListener, IListener
-{
- public static final String VIEW_ID = "org.eclipse.net4j.util.Net4jIntrospectorView"; //$NON-NLS-1$
-
- private static final Object[] NO_ELEMENTS = {};
-
- private static Net4jIntrospectorView instance;
-
- private TableViewer currentViewer;
-
- private TableViewer objectViewer;
-
- private TableViewer iterableViewer;
-
- private TableViewer arrayViewer;
-
- private TableViewer mapViewer;
-
- private Stack<Object> elements = new Stack<Object>();
-
- private Text classLabel;
-
- private Text objectLabel;
-
- private IAction backAction = new BackAction();
-
- private IAction containerAction = new ContainerAction();
-
- private IAction refreshAction = new RefreshAction();
-
- private StackLayout stackLayout;
-
- private Composite stacked;
-
- public Net4jIntrospectorView()
- {
- }
-
- @Override
- public void dispose()
- {
- getSite().getPage().removeSelectionListener(this);
- super.dispose();
- }
-
- public static Net4jIntrospectorView getInstance()
- {
- return instance;
- }
-
- public static synchronized Net4jIntrospectorView getInstance(boolean show)
- {
- if (instance == null)
- {
- try
- {
- IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
- page.showView(VIEW_ID);
- }
- catch (Exception ex)
- {
- throw WrappedException.wrap(ex);
- }
- }
-
- return instance;
- }
-
- @Override
- public void createPartControl(Composite parent)
- {
- Color bg = parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND);
- Color gray = parent.getDisplay().getSystemColor(SWT.COLOR_DARK_BLUE);
-
- Composite composite = new Composite(parent, SWT.NONE);
- composite.setLayout(UIUtil.createGridLayout(1));
-
- Composite c = new Composite(composite, SWT.BORDER);
- c.setLayout(UIUtil.createGridLayout(2));
- c.setLayoutData(UIUtil.createGridData(true, false));
-
- classLabel = new Text(c, SWT.READ_ONLY);
- classLabel.setLayoutData(UIUtil.createGridData(false, false));
- classLabel.setBackground(bg);
- classLabel.setForeground(gray);
-
- objectLabel = new Text(c, SWT.READ_ONLY);
- objectLabel.setLayoutData(UIUtil.createGridData(true, false));
- objectLabel.setBackground(bg);
-
- stackLayout = new StackLayout();
- stacked = new Composite(composite, SWT.NONE);
- stacked.setLayoutData(UIUtil.createGridData());
- stacked.setLayout(stackLayout);
-
- objectViewer = createViewer(stacked);
- createObjectColmuns();
- objectViewer.addDoubleClickListener(this);
- objectViewer.setContentProvider(new ObjectContentProvider());
- objectViewer.setLabelProvider(new ObjectLabelProvider());
- objectViewer.setSorter(new NameSorter());
- objectViewer.setInput(getViewSite());
-
- iterableViewer = createViewer(stacked);
- createIterableColmuns();
- iterableViewer.addDoubleClickListener(this);
- iterableViewer.setContentProvider(new IterableContentProvider());
- iterableViewer.setLabelProvider(new IterableLabelProvider());
- iterableViewer.setInput(getViewSite());
-
- arrayViewer = createViewer(stacked);
- createArrayColmuns();
- arrayViewer.addDoubleClickListener(this);
- arrayViewer.setContentProvider(new ArrayContentProvider());
- arrayViewer.setLabelProvider(new ArrayLabelProvider());
- arrayViewer.setInput(getViewSite());
-
- mapViewer = createViewer(stacked);
- createMapColmuns();
- mapViewer.addDoubleClickListener(this);
- mapViewer.setContentProvider(new MapContentProvider());
- mapViewer.setLabelProvider(new MapLabelProvider());
- mapViewer.setSorter(new NameSorter());
- mapViewer.setInput(getViewSite());
-
- IActionBars bars = getViewSite().getActionBars();
- fillLocalPullDown(bars.getMenuManager());
- fillLocalToolBar(bars.getToolBarManager());
- getSite().getPage().addSelectionListener(this);
- setCurrentViewer(objectViewer);
- instance = this;
- }
-
- private void setCurrentViewer(TableViewer viewer)
- {
- currentViewer = viewer;
- stackLayout.topControl = currentViewer.getControl();
- stacked.layout();
- }
-
- private TableViewer createViewer(Composite parent)
- {
- TableViewer viewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
- viewer.getTable().setLayoutData(UIUtil.createGridData());
- viewer.getTable().setHeaderVisible(true);
- viewer.getTable().setLinesVisible(true);
- return viewer;
- }
-
- public void refreshViewer()
- {
- UIUtil.refreshViewer(currentViewer);
- }
-
- public void selectionChanged(IWorkbenchPart part, ISelection sel)
- {
- if (part == this)
- {
- return;
- }
-
- if (sel instanceof IStructuredSelection)
- {
- IStructuredSelection ssel = (IStructuredSelection)sel;
- elements.clear();
- setObject(ssel.getFirstElement());
- }
- else
- {
- setObject(null);
- }
- }
-
- public void doubleClick(DoubleClickEvent event)
- {
- ISelection sel = event.getSelection();
- if (sel instanceof IStructuredSelection)
- {
- IStructuredSelection ssel = (IStructuredSelection)sel;
- Object element = ssel.getFirstElement();
- if (currentViewer == objectViewer && element instanceof Pair<?, ?>)
- {
- @SuppressWarnings("unchecked")
- Pair<Field, Object> pair = (Pair<Field, Object>)element;
-
- Field field = pair.getElement1();
- if (!field.getType().isPrimitive())
- {
- setObject(pair.getElement2());
- }
- }
- else if (currentViewer == mapViewer && element instanceof Map.Entry<?, ?>)
- {
- Map.Entry<?, ?> entry = (Map.Entry<?, ?>)element;
- setObject(entry.getValue());
- }
- else if (currentViewer == iterableViewer)
- {
- setObject(element);
- }
- else if (currentViewer == arrayViewer && element instanceof Pair<?, ?>)
- {
- @SuppressWarnings("unchecked")
- Pair<Integer, Object> pair = (Pair<Integer, Object>)element;
- setObject(pair.getElement2());
- }
- }
- }
-
- /**
- * Passing the focus request to the viewer's control.
- */
- @Override
- public void setFocus()
- {
- try
- {
- currentViewer.getControl().setFocus();
- }
- catch (RuntimeException ignore)
- {
- }
- }
-
- public void notifyEvent(IEvent event)
- {
- refreshViewer();
- }
-
- public void setObject(Object object)
- {
- EventUtil.removeListener(object, this);
- if (object != null)
- {
- if (!elements.isEmpty())
- {
- Object element = elements.peek();
- if (element != object)
- {
- EventUtil.removeListener(element, this);
- elements.push(object);
- }
- }
- else
- {
- elements.push(object);
- }
- }
-
- if (object == null)
- {
- classLabel.setText(""); //$NON-NLS-1$
- objectLabel.setText(""); //$NON-NLS-1$
- currentViewer = objectViewer;
- }
- else
- {
- EventUtil.addListener(object, this);
- String className = object.getClass().getName();
- classLabel.setText(className);
-
- String value = object.toString();
- if (value.startsWith(className + "@")) //$NON-NLS-1$
- {
- objectLabel.setText(value.substring(className.length()));
- }
- else
- {
- objectLabel.setText(value);
- }
- }
-
- classLabel.getParent().layout();
- backAction.setEnabled(elements.size() >= 2);
-
- if (object instanceof Map<?, ?>)
- {
- setCurrentViewer(mapViewer);
- }
- else if (object instanceof Iterable<?>)
- {
- setCurrentViewer(iterableViewer);
- }
- else if (object != null && object.getClass().isArray())
- {
- setCurrentViewer(arrayViewer);
- }
- else
- {
- setCurrentViewer(objectViewer);
- }
-
- refreshViewer();
- }
-
- private void createObjectColmuns()
- {
- Table table = objectViewer.getTable();
- String[] columnNames = {
- Messages.getString("Net4jIntrospectorView_4"), Messages.getString("Net4jIntrospectorView_5"), Messages.getString("Net4jIntrospectorView_6"), Messages.getString("Net4jIntrospectorView_7") }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
- int[] columnWidths = { 200, 400, 300, 300 };
- createColumns(table, columnNames, columnWidths);
- }
-
- private void createMapColmuns()
- {
- Table table = mapViewer.getTable();
- String[] columnNames = {
- Messages.getString("Net4jIntrospectorView_8"), Messages.getString("Net4jIntrospectorView_9"), Messages.getString("Net4jIntrospectorView_10") }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- int[] columnWidths = { 200, 400, 300 };
- createColumns(table, columnNames, columnWidths);
- }
-
- private void createIterableColmuns()
- {
- Table table = iterableViewer.getTable();
- String[] columnNames = {
- Messages.getString("Net4jIntrospectorView_11"), Messages.getString("Net4jIntrospectorView_12") }; //$NON-NLS-1$ //$NON-NLS-2$
- int[] columnWidths = { 400, 300 };
- createColumns(table, columnNames, columnWidths);
- }
-
- private void createArrayColmuns()
- {
- Table table = arrayViewer.getTable();
- String[] columnNames = {
- Messages.getString("Net4jIntrospectorView_13"), Messages.getString("Net4jIntrospectorView_14"), Messages.getString("Net4jIntrospectorView_15") }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- int[] columnWidths = { 50, 400, 300 };
- createColumns(table, columnNames, columnWidths);
- }
-
- private void createColumns(Table table, String[] columnNames, int[] columnWidths)
- {
- TableColumn[] columns = new TableColumn[columnNames.length];
- for (int i = 0; i < columns.length; i++)
- {
- TableColumn column = new TableColumn(table, SWT.LEFT, i);
- column.setText(columnNames[i]);
- column.setWidth(columnWidths[i]);
- column.setMoveable(true);
- column.setResizable(true);
- }
- }
-
- private void fillLocalPullDown(IMenuManager manager)
- {
- }
-
- private void fillLocalToolBar(IToolBarManager manager)
- {
- manager.add(backAction);
- manager.add(containerAction);
- manager.add(refreshAction);
- }
-
- /**
- * @author Eike Stepper
- */
- class BackAction extends Action
- {
- private BackAction()
- {
- super(Messages.getString("Net4jIntrospectorView_16")); //$NON-NLS-1$
- ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
- setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_BACK));
- setDisabledImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_BACK_DISABLED));
- }
-
- @Override
- public void run()
- {
- if (!elements.isEmpty())
- {
- elements.pop();
- if (!elements.isEmpty())
- {
- setObject(elements.peek());
- }
- }
- }
- }
-
- /**
- * @author Eike Stepper
- */
- class ContainerAction extends Action
- {
- private ContainerAction()
- {
- super(Messages.getString("Net4jIntrospectorView_17")); //$NON-NLS-1$
- setImageDescriptor(SharedIcons.getDescriptor(SharedIcons.VIEW_CONTAINER));
- }
-
- @Override
- public void run()
- {
- setObject(IPluginContainer.INSTANCE);
- }
- }
-
- /**
- * @author Eike Stepper
- */
- class RefreshAction extends Action
- {
- private RefreshAction()
- {
- super("Refresh"); //$NON-NLS-1$
- setImageDescriptor(SharedIcons.getDescriptor(SharedIcons.ETOOL_REFRESH));
- }
-
- @Override
- public void run()
- {
- refreshViewer();
- }
- }
-
- /**
- * @author Eike Stepper
- */
- abstract class AbstractContentProvider implements IStructuredContentProvider
- {
- public void inputChanged(Viewer v, Object oldInput, Object newInput)
- {
- }
-
- public void dispose()
- {
- }
- }
-
- /**
- * @author Eike Stepper
- */
- abstract class AbstractLabelProvider extends LabelProvider implements ITableLabelProvider
- {
- @Override
- public String getText(Object element)
- {
- return getColumnText(element, 0);
- }
-
- public Image getColumnImage(Object obj, int index)
- {
- return null;
- }
-
- @Override
- public Image getImage(Object obj)
- {
- return null;
- }
- }
-
- /**
- * @author Eike Stepper
- */
- class ObjectContentProvider extends AbstractContentProvider
- {
- public Object[] getElements(Object parent)
- {
- if (!elements.isEmpty())
- {
- try
- {
- return ReflectUtil.dumpToArray(elements.peek());
- }
- catch (RuntimeException ex)
- {
- OM.LOG.error(ex);
- }
- }
-
- return NO_ELEMENTS;
- }
- }
-
- /**
- * @author Eike Stepper
- */
- class ObjectLabelProvider extends AbstractLabelProvider
- {
- public String getColumnText(Object obj, int index)
- {
- if (obj instanceof Pair<?, ?>)
- {
- try
- {
- @SuppressWarnings("unchecked")
- Pair<Field, Object> pair = (Pair<Field, Object>)obj;
-
- Field field = pair.getElement1();
- Object value = pair.getElement2();
-
- switch (index)
- {
- case 0:
- return field.getName();
- case 1:
- return value == null ? Messages.getString("Net4jIntrospectorView_18") : value.toString(); //$NON-NLS-1$
- case 2:
- return field.getType().getName();
- case 3:
- return value == null ? Messages.getString("Net4jIntrospectorView_1") : value.getClass().getName(); //$NON-NLS-1$
- }
- }
- catch (RuntimeException ex)
- {
- OM.LOG.error(ex);
- }
- }
-
- return ""; //$NON-NLS-1$
- }
- }
-
- /**
- * @author Eike Stepper
- */
- class IterableContentProvider extends AbstractContentProvider
- {
- public Object[] getElements(Object parent)
- {
- if (!elements.isEmpty())
- {
- Object element = elements.peek();
- if (element instanceof Iterable<?>)
- {
- List<Object> result = new ArrayList<Object>();
- for (Object object : (Iterable<?>)element)
- {
- result.add(object);
- }
-
- return result.toArray();
- }
- }
-
- return NO_ELEMENTS;
- }
- }
-
- /**
- * @author Eike Stepper
- */
- class IterableLabelProvider extends AbstractLabelProvider
- {
- public String getColumnText(Object obj, int index)
- {
- switch (index)
- {
- case 0:
- return obj == null ? Messages.getString("Net4jIntrospectorView_21") : obj.toString(); //$NON-NLS-1$
- case 1:
- return obj == null ? Messages.getString("Net4jIntrospectorView_22") : obj.getClass().getName(); //$NON-NLS-1$
- }
-
- return ""; //$NON-NLS-1$
- }
- }
-
- /**
- * @author Eike Stepper
- */
- class ArrayContentProvider extends AbstractContentProvider
- {
- @SuppressWarnings("unchecked")
- public Object[] getElements(Object parent)
- {
- if (!elements.isEmpty())
- {
- Object element = elements.peek();
- if (element.getClass().isArray())
- {
- Object[] array = (Object[])element;
- Pair<Integer, Object>[] result = new Pair[array.length];
- for (int i = 0; i < array.length; i++)
- {
- result[i] = new Pair<Integer, Object>(i, array[i]);
- }
-
- return result;
- }
- }
-
- return NO_ELEMENTS;
- }
- }
-
- /**
- * @author Eike Stepper
- */
- class ArrayLabelProvider extends AbstractLabelProvider
- {
- public String getColumnText(Object obj, int index)
- {
- if (obj instanceof Pair<?, ?>)
- {
- try
- {
- @SuppressWarnings("unchecked")
- Pair<Integer, Object> pair = (Pair<Integer, Object>)obj;
-
- int i = pair.getElement1();
- Object value = pair.getElement2();
- switch (index)
- {
- case 0:
- return String.valueOf(i);
- case 1:
- return value == null ? Messages.getString("Net4jIntrospectorView_24") : value.toString(); //$NON-NLS-1$
- case 2:
- return value == null ? Messages.getString("Net4jIntrospectorView_25") : value.getClass().getName(); //$NON-NLS-1$
- }
- }
- catch (RuntimeException ex)
- {
- OM.LOG.error(ex);
- }
- }
-
- return ""; //$NON-NLS-1$
- }
- }
-
- /**
- * @author Eike Stepper
- */
- class MapContentProvider extends AbstractContentProvider
- {
- public Object[] getElements(Object parent)
- {
- if (!elements.isEmpty())
- {
- Object element = elements.peek();
- if (element instanceof Map<?, ?>)
- {
- return ((Map<?, ?>)element).entrySet().toArray();
- }
- }
-
- return NO_ELEMENTS;
- }
- }
-
- /**
- * @author Eike Stepper
- */
- class MapLabelProvider extends AbstractLabelProvider
- {
- public String getColumnText(Object obj, int index)
- {
- if (obj instanceof Map.Entry<?, ?>)
- {
- Map.Entry<?, ?> entry = (Map.Entry<?, ?>)obj;
- Object key = entry.getKey();
- Object value = entry.getValue();
- switch (index)
- {
- case 0:
- return key == null ? Messages.getString("Net4jIntrospectorView_27") : key.toString(); //$NON-NLS-1$
- case 1:
- return value == null ? Messages.getString("Net4jIntrospectorView_28") : value.toString(); //$NON-NLS-1$
- case 2:
- return value == null ? Messages.getString("Net4jIntrospectorView_29") : value.getClass().getName(); //$NON-NLS-1$
- }
- }
-
- return ""; //$NON-NLS-1$
- }
- }
-
- /**
- * @author Eike Stepper
- */
- class NameSorter extends ViewerSorter
- {
- }
-}
+/*
+ * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ */
+package org.eclipse.net4j.util.internal.ui.views;
+
+import org.eclipse.net4j.ui.shared.SharedIcons;
+import org.eclipse.net4j.util.ReflectUtil;
+import org.eclipse.net4j.util.WrappedException;
+import org.eclipse.net4j.util.collection.Pair;
+import org.eclipse.net4j.util.container.IPluginContainer;
+import org.eclipse.net4j.util.event.EventUtil;
+import org.eclipse.net4j.util.event.IEvent;
+import org.eclipse.net4j.util.event.IListener;
+import org.eclipse.net4j.util.internal.ui.bundle.OM;
+import org.eclipse.net4j.util.internal.ui.messages.Messages;
+import org.eclipse.net4j.util.ui.UIUtil;
+
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.action.IToolBarManager;
+import org.eclipse.jface.action.Separator;
+import org.eclipse.jface.viewers.DoubleClickEvent;
+import org.eclipse.jface.viewers.IDoubleClickListener;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredContentProvider;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.ITableLabelProvider;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.jface.viewers.ViewerSorter;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.StackLayout;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Table;
+import org.eclipse.swt.widgets.TableColumn;
+import org.eclipse.swt.widgets.Text;
+import org.eclipse.ui.IActionBars;
+import org.eclipse.ui.IPartListener;
+import org.eclipse.ui.ISelectionListener;
+import org.eclipse.ui.ISharedImages;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.part.ViewPart;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Stack;
+
+/**
+ * @author Eike Stepper
+ */
+public class Net4jIntrospectorView extends ViewPart implements IPartListener, ISelectionListener, IDoubleClickListener,
+ IListener
+{
+ public static final String VIEW_ID = "org.eclipse.net4j.util.Net4jIntrospectorView"; //$NON-NLS-1$
+
+ private static final Object[] NO_ELEMENTS = {};
+
+ private static Net4jIntrospectorView instance;
+
+ private TableViewer currentViewer;
+
+ private TableViewer objectViewer;
+
+ private TableViewer iterableViewer;
+
+ private TableViewer arrayViewer;
+
+ private TableViewer mapViewer;
+
+ private Stack<Object> elements = new Stack<Object>();
+
+ private Text classLabel;
+
+ private Text objectLabel;
+
+ private IAction backAction = new BackAction();
+
+ private IAction modeAction = new ModeAction();
+
+ private IAction containerAction = new ContainerAction();
+
+ private IAction refreshAction = new RefreshAction();
+
+ private StackLayout stackLayout;
+
+ private Composite stacked;
+
+ private IWorkbenchPart activePart;
+
+ public Net4jIntrospectorView()
+ {
+ }
+
+ @Override
+ public void dispose()
+ {
+ IWorkbenchPage page = getSite().getPage();
+ page.removePartListener(this);
+ page.removeSelectionListener(this);
+ super.dispose();
+ }
+
+ public static Net4jIntrospectorView getInstance()
+ {
+ return instance;
+ }
+
+ public static synchronized Net4jIntrospectorView getInstance(boolean show)
+ {
+ if (instance == null)
+ {
+ try
+ {
+ IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+ page.showView(VIEW_ID);
+ }
+ catch (Exception ex)
+ {
+ throw WrappedException.wrap(ex);
+ }
+ }
+
+ return instance;
+ }
+
+ @Override
+ public void createPartControl(Composite parent)
+ {
+ Color bg = parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND);
+ Color gray = parent.getDisplay().getSystemColor(SWT.COLOR_DARK_BLUE);
+
+ Composite composite = new Composite(parent, SWT.NONE);
+ composite.setLayout(UIUtil.createGridLayout(1));
+
+ Composite c = new Composite(composite, SWT.BORDER);
+ c.setLayout(UIUtil.createGridLayout(2));
+ c.setLayoutData(UIUtil.createGridData(true, false));
+
+ classLabel = new Text(c, SWT.READ_ONLY);
+ classLabel.setLayoutData(UIUtil.createGridData(false, false));
+ classLabel.setBackground(bg);
+ classLabel.setForeground(gray);
+
+ objectLabel = new Text(c, SWT.READ_ONLY);
+ objectLabel.setLayoutData(UIUtil.createGridData(true, false));
+ objectLabel.setBackground(bg);
+
+ stackLayout = new StackLayout();
+ stacked = new Composite(composite, SWT.NONE);
+ stacked.setLayoutData(UIUtil.createGridData());
+ stacked.setLayout(stackLayout);
+
+ objectViewer = createViewer(stacked);
+ createObjectColmuns();
+ objectViewer.addDoubleClickListener(this);
+ objectViewer.setContentProvider(new ObjectContentProvider());
+ objectViewer.setLabelProvider(new ObjectLabelProvider());
+ objectViewer.setSorter(new NameSorter());
+ objectViewer.setInput(getViewSite());
+
+ iterableViewer = createViewer(stacked);
+ createIterableColmuns();
+ iterableViewer.addDoubleClickListener(this);
+ iterableViewer.setContentProvider(new IterableContentProvider());
+ iterableViewer.setLabelProvider(new IterableLabelProvider());
+ iterableViewer.setInput(getViewSite());
+
+ arrayViewer = createViewer(stacked);
+ createArrayColmuns();
+ arrayViewer.addDoubleClickListener(this);
+ arrayViewer.setContentProvider(new ArrayContentProvider());
+ arrayViewer.setLabelProvider(new ArrayLabelProvider());
+ arrayViewer.setInput(getViewSite());
+
+ mapViewer = createViewer(stacked);
+ createMapColmuns();
+ mapViewer.addDoubleClickListener(this);
+ mapViewer.setContentProvider(new MapContentProvider());
+ mapViewer.setLabelProvider(new MapLabelProvider());
+ mapViewer.setSorter(new NameSorter());
+ mapViewer.setInput(getViewSite());
+
+ IActionBars bars = getViewSite().getActionBars();
+ fillLocalPullDown(bars.getMenuManager());
+ fillLocalToolBar(bars.getToolBarManager());
+
+ IWorkbenchPage page = getSite().getPage();
+ page.addPartListener(this);
+ page.addSelectionListener(this);
+
+ setCurrentViewer(objectViewer);
+ instance = this;
+ }
+
+ private void setCurrentViewer(TableViewer viewer)
+ {
+ currentViewer = viewer;
+ stackLayout.topControl = currentViewer.getControl();
+ stacked.layout();
+ }
+
+ private TableViewer createViewer(Composite parent)
+ {
+ TableViewer viewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
+ viewer.getTable().setLayoutData(UIUtil.createGridData());
+ viewer.getTable().setHeaderVisible(true);
+ viewer.getTable().setLinesVisible(true);
+ return viewer;
+ }
+
+ public void refreshViewer()
+ {
+ UIUtil.refreshViewer(currentViewer);
+ }
+
+ public void partActivated(IWorkbenchPart part)
+ {
+ if (part == this)
+ {
+ return;
+ }
+
+ activePart = part;
+ if (modeAction.isChecked())
+ {
+ elements.clear();
+ setObject(activePart);
+ }
+ }
+
+ public void partBroughtToTop(IWorkbenchPart part)
+ {
+ }
+
+ public void partClosed(IWorkbenchPart part)
+ {
+ }
+
+ public void partDeactivated(IWorkbenchPart part)
+ {
+ if (modeAction.isChecked())
+ {
+ if (part == activePart)
+ {
+ activePart = null;
+ setObject(null);
+ }
+ }
+ }
+
+ public void partOpened(IWorkbenchPart part)
+ {
+ }
+
+ public void selectionChanged(IWorkbenchPart part, ISelection sel)
+ {
+ if (part == this)
+ {
+ return;
+ }
+
+ if (modeAction.isChecked())
+ {
+ return;
+ }
+
+ if (sel instanceof IStructuredSelection)
+ {
+ IStructuredSelection ssel = (IStructuredSelection)sel;
+ elements.clear();
+ setObject(ssel.getFirstElement());
+ }
+ else
+ {
+ setObject(null);
+ }
+ }
+
+ public void doubleClick(DoubleClickEvent event)
+ {
+ ISelection sel = event.getSelection();
+ if (sel instanceof IStructuredSelection)
+ {
+ IStructuredSelection ssel = (IStructuredSelection)sel;
+ Object element = ssel.getFirstElement();
+ if (currentViewer == objectViewer && element instanceof Pair<?, ?>)
+ {
+ @SuppressWarnings("unchecked")
+ Pair<Field, Object> pair = (Pair<Field, Object>)element;
+
+ Field field = pair.getElement1();
+ if (!field.getType().isPrimitive())
+ {
+ setObject(pair.getElement2());
+ }
+ }
+ else if (currentViewer == mapViewer && element instanceof Map.Entry<?, ?>)
+ {
+ Map.Entry<?, ?> entry = (Map.Entry<?, ?>)element;
+ setObject(entry.getValue());
+ }
+ else if (currentViewer == iterableViewer)
+ {
+ setObject(element);
+ }
+ else if (currentViewer == arrayViewer && element instanceof Pair<?, ?>)
+ {
+ @SuppressWarnings("unchecked")
+ Pair<Integer, Object> pair = (Pair<Integer, Object>)element;
+ setObject(pair.getElement2());
+ }
+ }
+ }
+
+ /**
+ * Passing the focus request to the viewer's control.
+ */
+ @Override
+ public void setFocus()
+ {
+ try
+ {
+ currentViewer.getControl().setFocus();
+ }
+ catch (RuntimeException ignore)
+ {
+ }
+ }
+
+ public void notifyEvent(IEvent event)
+ {
+ refreshViewer();
+ }
+
+ public void setObject(Object object)
+ {
+ EventUtil.removeListener(object, this);
+ if (object != null)
+ {
+ if (!elements.isEmpty())
+ {
+ Object element = elements.peek();
+ if (element != object)
+ {
+ EventUtil.removeListener(element, this);
+ elements.push(object);
+ }
+ }
+ else
+ {
+ elements.push(object);
+ }
+ }
+
+ if (object == null)
+ {
+ classLabel.setText(""); //$NON-NLS-1$
+ objectLabel.setText(""); //$NON-NLS-1$
+ currentViewer = objectViewer;
+ }
+ else
+ {
+ EventUtil.addListener(object, this);
+ String className = object.getClass().getName();
+ classLabel.setText(className);
+
+ String value = object.toString();
+ if (value.startsWith(className + "@")) //$NON-NLS-1$
+ {
+ objectLabel.setText(value.substring(className.length()));
+ }
+ else
+ {
+ objectLabel.setText(value);
+ }
+ }
+
+ classLabel.getParent().layout();
+ backAction.setEnabled(elements.size() >= 2);
+
+ if (object instanceof Map<?, ?>)
+ {
+ setCurrentViewer(mapViewer);
+ }
+ else if (object instanceof Iterable<?>)
+ {
+ setCurrentViewer(iterableViewer);
+ }
+ else if (object != null && object.getClass().isArray())
+ {
+ setCurrentViewer(arrayViewer);
+ }
+ else
+ {
+ setCurrentViewer(objectViewer);
+ }
+
+ refreshViewer();
+ }
+
+ private void createObjectColmuns()
+ {
+ Table table = objectViewer.getTable();
+ String[] columnNames = {
+ Messages.getString("Net4jIntrospectorView_4"), Messages.getString("Net4jIntrospectorView_5"), Messages.getString("Net4jIntrospectorView_6"), Messages.getString("Net4jIntrospectorView_7") }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ int[] columnWidths = { 200, 400, 300, 300 };
+ createColumns(table, columnNames, columnWidths);
+ }
+
+ private void createMapColmuns()
+ {
+ Table table = mapViewer.getTable();
+ String[] columnNames = {
+ Messages.getString("Net4jIntrospectorView_8"), Messages.getString("Net4jIntrospectorView_9"), Messages.getString("Net4jIntrospectorView_10") }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ int[] columnWidths = { 200, 400, 300 };
+ createColumns(table, columnNames, columnWidths);
+ }
+
+ private void createIterableColmuns()
+ {
+ Table table = iterableViewer.getTable();
+ String[] columnNames = {
+ Messages.getString("Net4jIntrospectorView_11"), Messages.getString("Net4jIntrospectorView_12") }; //$NON-NLS-1$ //$NON-NLS-2$
+ int[] columnWidths = { 400, 300 };
+ createColumns(table, columnNames, columnWidths);
+ }
+
+ private void createArrayColmuns()
+ {
+ Table table = arrayViewer.getTable();
+ String[] columnNames = {
+ Messages.getString("Net4jIntrospectorView_13"), Messages.getString("Net4jIntrospectorView_14"), Messages.getString("Net4jIntrospectorView_15") }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ int[] columnWidths = { 50, 400, 300 };
+ createColumns(table, columnNames, columnWidths);
+ }
+
+ private void createColumns(Table table, String[] columnNames, int[] columnWidths)
+ {
+ TableColumn[] columns = new TableColumn[columnNames.length];
+ for (int i = 0; i < columns.length; i++)
+ {
+ TableColumn column = new TableColumn(table, SWT.LEFT, i);
+ column.setText(columnNames[i]);
+ column.setWidth(columnWidths[i]);
+ column.setMoveable(true);
+ column.setResizable(true);
+ }
+ }
+
+ private void fillLocalPullDown(IMenuManager manager)
+ {
+ }
+
+ private void fillLocalToolBar(IToolBarManager manager)
+ {
+ manager.add(backAction);
+ manager.add(containerAction);
+ manager.add(new Separator());
+ manager.add(modeAction);
+ manager.add(new Separator());
+ manager.add(refreshAction);
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ class BackAction extends Action
+ {
+ private BackAction()
+ {
+ super(Messages.getString("Net4jIntrospectorView_16")); //$NON-NLS-1$
+ ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
+ setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_BACK));
+ setDisabledImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_BACK_DISABLED));
+ }
+
+ @Override
+ public void run()
+ {
+ if (!elements.isEmpty())
+ {
+ elements.pop();
+ if (!elements.isEmpty())
+ {
+ setObject(elements.peek());
+ }
+ }
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ class ModeAction extends Action
+ {
+ private ModeAction()
+ {
+ super(Messages.getString("Net4jIntrospectorView_17b"), AS_CHECK_BOX); //$NON-NLS-1$
+ setImageDescriptor(SharedIcons.getDescriptor(SharedIcons.ETOOL_PART_MODE));
+ }
+
+ @Override
+ public void run()
+ {
+ if (isChecked())
+ {
+ elements.clear();
+ setObject(activePart);
+ }
+ else
+ {
+ setObject(null);
+ }
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ class ContainerAction extends Action
+ {
+ private ContainerAction()
+ {
+ super(Messages.getString("Net4jIntrospectorView_17")); //$NON-NLS-1$
+ setImageDescriptor(SharedIcons.getDescriptor(SharedIcons.VIEW_CONTAINER));
+ }
+
+ @Override
+ public void run()
+ {
+ setObject(IPluginContainer.INSTANCE);
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ class RefreshAction extends Action
+ {
+ private RefreshAction()
+ {
+ super("Refresh"); //$NON-NLS-1$
+ setImageDescriptor(SharedIcons.getDescriptor(SharedIcons.ETOOL_REFRESH));
+ }
+
+ @Override
+ public void run()
+ {
+ refreshViewer();
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ abstract class AbstractContentProvider implements IStructuredContentProvider
+ {
+ public void inputChanged(Viewer v, Object oldInput, Object newInput)
+ {
+ }
+
+ public void dispose()
+ {
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ abstract class AbstractLabelProvider extends LabelProvider implements ITableLabelProvider
+ {
+ @Override
+ public String getText(Object element)
+ {
+ return getColumnText(element, 0);
+ }
+
+ public Image getColumnImage(Object obj, int index)
+ {
+ return null;
+ }
+
+ @Override
+ public Image getImage(Object obj)
+ {
+ return null;
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ class ObjectContentProvider extends AbstractContentProvider
+ {
+ public Object[] getElements(Object parent)
+ {
+ if (!elements.isEmpty())
+ {
+ try
+ {
+ return ReflectUtil.dumpToArray(elements.peek());
+ }
+ catch (RuntimeException ex)
+ {
+ OM.LOG.error(ex);
+ }
+ }
+
+ return NO_ELEMENTS;
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ class ObjectLabelProvider extends AbstractLabelProvider
+ {
+ public String getColumnText(Object obj, int index)
+ {
+ if (obj instanceof Pair<?, ?>)
+ {
+ try
+ {
+ @SuppressWarnings("unchecked")
+ Pair<Field, Object> pair = (Pair<Field, Object>)obj;
+
+ Field field = pair.getElement1();
+ Object value = pair.getElement2();
+
+ switch (index)
+ {
+ case 0:
+ return field.getName();
+ case 1:
+ return value == null ? Messages.getString("Net4jIntrospectorView_18") : value.toString(); //$NON-NLS-1$
+ case 2:
+ return field.getType().getName();
+ case 3:
+ return value == null ? Messages.getString("Net4jIntrospectorView_1") : value.getClass().getName(); //$NON-NLS-1$
+ }
+ }
+ catch (RuntimeException ex)
+ {
+ OM.LOG.error(ex);
+ }
+ }
+
+ return ""; //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ class IterableContentProvider extends AbstractContentProvider
+ {
+ public Object[] getElements(Object parent)
+ {
+ if (!elements.isEmpty())
+ {
+ Object element = elements.peek();
+ if (element instanceof Iterable<?>)
+ {
+ List<Object> result = new ArrayList<Object>();
+ for (Object object : (Iterable<?>)element)
+ {
+ result.add(object);
+ }
+
+ return result.toArray();
+ }
+ }
+
+ return NO_ELEMENTS;
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ class IterableLabelProvider extends AbstractLabelProvider
+ {
+ public String getColumnText(Object obj, int index)
+ {
+ switch (index)
+ {
+ case 0:
+ return obj == null ? Messages.getString("Net4jIntrospectorView_21") : obj.toString(); //$NON-NLS-1$
+ case 1:
+ return obj == null ? Messages.getString("Net4jIntrospectorView_22") : obj.getClass().getName(); //$NON-NLS-1$
+ }
+
+ return ""; //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ class ArrayContentProvider extends AbstractContentProvider
+ {
+ @SuppressWarnings("unchecked")
+ public Object[] getElements(Object parent)
+ {
+ if (!elements.isEmpty())
+ {
+ Object element = elements.peek();
+ if (element.getClass().isArray())
+ {
+ Object[] array = (Object[])element;
+ Pair<Integer, Object>[] result = new Pair[array.length];
+ for (int i = 0; i < array.length; i++)
+ {
+ result[i] = new Pair<Integer, Object>(i, array[i]);
+ }
+
+ return result;
+ }
+ }
+
+ return NO_ELEMENTS;
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ class ArrayLabelProvider extends AbstractLabelProvider
+ {
+ public String getColumnText(Object obj, int index)
+ {
+ if (obj instanceof Pair<?, ?>)
+ {
+ try
+ {
+ @SuppressWarnings("unchecked")
+ Pair<Integer, Object> pair = (Pair<Integer, Object>)obj;
+
+ int i = pair.getElement1();
+ Object value = pair.getElement2();
+ switch (index)
+ {
+ case 0:
+ return String.valueOf(i);
+ case 1:
+ return value == null ? Messages.getString("Net4jIntrospectorView_24") : value.toString(); //$NON-NLS-1$
+ case 2:
+ return value == null ? Messages.getString("Net4jIntrospectorView_25") : value.getClass().getName(); //$NON-NLS-1$
+ }
+ }
+ catch (RuntimeException ex)
+ {
+ OM.LOG.error(ex);
+ }
+ }
+
+ return ""; //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ class MapContentProvider extends AbstractContentProvider
+ {
+ public Object[] getElements(Object parent)
+ {
+ if (!elements.isEmpty())
+ {
+ Object element = elements.peek();
+ if (element instanceof Map<?, ?>)
+ {
+ return ((Map<?, ?>)element).entrySet().toArray();
+ }
+ }
+
+ return NO_ELEMENTS;
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ class MapLabelProvider extends AbstractLabelProvider
+ {
+ public String getColumnText(Object obj, int index)
+ {
+ if (obj instanceof Map.Entry<?, ?>)
+ {
+ Map.Entry<?, ?> entry = (Map.Entry<?, ?>)obj;
+ Object key = entry.getKey();
+ Object value = entry.getValue();
+ switch (index)
+ {
+ case 0:
+ return key == null ? Messages.getString("Net4jIntrospectorView_27") : key.toString(); //$NON-NLS-1$
+ case 1:
+ return value == null ? Messages.getString("Net4jIntrospectorView_28") : value.toString(); //$NON-NLS-1$
+ case 2:
+ return value == null ? Messages.getString("Net4jIntrospectorView_29") : value.getClass().getName(); //$NON-NLS-1$
+ }
+ }
+
+ return ""; //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ class NameSorter extends ViewerSorter
+ {
+ }
+}
diff --git a/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/ui/FactoryTypeContentProvider.java b/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/ui/FactoryTypeContentProvider.java
new file mode 100644
index 0000000000..a9872d7f46
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/ui/FactoryTypeContentProvider.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ */
+package org.eclipse.net4j.util.ui;
+
+import org.eclipse.net4j.util.container.IManagedContainer;
+import org.eclipse.net4j.util.factory.IFactoryKey;
+
+/**
+ * A {@link StructuredContentProvider structured content provider} that shows the {@link IFactoryKey#getType() factory
+ * types} of the {@link #getInput() input}.
+ *
+ * @author Eike Stepper
+ * @since 3.2
+ */
+public class FactoryTypeContentProvider extends StructuredContentProvider<IManagedContainer>
+{
+ private String productGroup;
+
+ public FactoryTypeContentProvider(String productGroup)
+ {
+ this.productGroup = productGroup;
+ }
+
+ public String getProductGroup()
+ {
+ return productGroup;
+ }
+
+ public Object[] getElements(Object inputElement)
+ {
+ return getInput().getFactoryTypes(productGroup).toArray();
+ }
+
+ @Override
+ protected void connectInput(IManagedContainer input)
+ {
+ input.addListener(this);
+ input.getFactoryRegistry().addListener(this);
+ }
+
+ @Override
+ protected void disconnectInput(IManagedContainer input)
+ {
+ input.removeListener(this);
+ input.getFactoryRegistry().removeListener(this);
+ }
+}
diff --git a/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/ui/container/ElementWizardAction.java b/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/ui/container/ElementWizardAction.java
index a6c5f0160b..b0ac24d7b8 100644
--- a/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/ui/container/ElementWizardAction.java
+++ b/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/ui/container/ElementWizardAction.java
@@ -111,7 +111,8 @@ public class ElementWizardAction extends LongRunningAction
{
public void run()
{
- MessageDialog.openError(shell, title, "An error occured: " + ex.getMessage());
+ MessageDialog.openError(shell, title, "An error occured: " + ex.getMessage()
+ + "\n\nThe error log may contain more information about the problem.");
}
});
}
diff --git a/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/ui/views/ContainerItemProvider.java b/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/ui/views/ContainerItemProvider.java
index e68ece4e93..e23fd48060 100644
--- a/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/ui/views/ContainerItemProvider.java
+++ b/plugins/org.eclipse.net4j.util.ui/src/org/eclipse/net4j/util/ui/views/ContainerItemProvider.java
@@ -1,721 +1,707 @@
-/*
- * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Eike Stepper - initial API and implementation
- */
-package org.eclipse.net4j.util.ui.views;
-
-import org.eclipse.net4j.internal.util.bundle.OM;
-import org.eclipse.net4j.ui.shared.SharedIcons;
-import org.eclipse.net4j.util.container.ContainerEventAdapter;
-import org.eclipse.net4j.util.container.IContainer;
-import org.eclipse.net4j.util.container.ISlow;
-import org.eclipse.net4j.util.event.EventUtil;
-import org.eclipse.net4j.util.event.IEvent;
-import org.eclipse.net4j.util.event.IListener;
-import org.eclipse.net4j.util.lifecycle.LifecycleState;
-import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
-import org.eclipse.net4j.util.ui.UIUtil;
-
-import org.eclipse.jface.action.Action;
-import org.eclipse.jface.action.IMenuManager;
-import org.eclipse.jface.viewers.ITreeSelection;
-import org.eclipse.jface.viewers.TreePath;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.graphics.Font;
-import org.eclipse.swt.graphics.Image;
-import org.eclipse.ui.PartInitException;
-
-import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-/**
- * @author Eike Stepper
- */
-/**
- * @author Eike Stepper
- */
-public class ContainerItemProvider<CONTAINER extends IContainer<Object>> extends ItemProvider<CONTAINER>
-{
- private Map<Object, Node> nodes = new HashMap<Object, Node>();
-
- private Node root;
-
- private IElementFilter rootElementFilter;
-
- public ContainerItemProvider()
- {
- }
-
- public ContainerItemProvider(IElementFilter rootElementFilter)
- {
- this.rootElementFilter = rootElementFilter;
- }
-
- public IElementFilter getRootElementFilter()
- {
- return rootElementFilter;
- }
-
- @Override
- public boolean hasChildren(Object element)
- {
- try
- {
- Node node = getNode(element);
- return !node.getChildren().isEmpty();
- }
- catch (Exception ex)
- {
- return false;
- }
- }
-
- public Object[] getChildren(Object element)
- {
- try
- {
- Node node = getNode(element);
- List<Node> children = node.getChildren();
- for (Iterator<Node> it = children.iterator(); it.hasNext();)
- {
- Node child = it.next();
- if (child.isDisposed())
- {
- it.remove();
- }
- else
- {
- Object childElement = child.getElement();
- LifecycleState lifecycleState = LifecycleUtil.getLifecycleState(childElement);
- if (lifecycleState == LifecycleState.INACTIVE || lifecycleState == LifecycleState.DEACTIVATING)
- {
- handleInactiveElement(it, child);
- }
- }
- }
-
- Object[] result = new Object[children.size()];
- for (int i = 0; i < result.length; i++)
- {
- result[i] = children.get(i).getElement();
- }
-
- return result;
- }
- catch (RuntimeException ex)
- {
- return NO_ELEMENTS;
- }
- }
-
- public Object getParent(Object element)
- {
- try
- {
- Node node = getNode(element);
- Node parentNode = node.getParent();
- return parentNode == null ? null : parentNode.getElement();
- }
- catch (RuntimeException ex)
- {
- return null;
- }
- }
-
- @Override
- public void dispose()
- {
- super.dispose();
- }
-
- @Override
- protected void connectInput(CONTAINER input)
- {
- root = createNode(null, input);
- if (root != null)
- {
- addNode(input, root);
- }
- }
-
- @Override
- protected void disconnectInput(CONTAINER input)
- {
- root.dispose();
- root = null;
- nodes.clear();
- }
-
- /**
- * @since 2.0
- */
- protected void handleInactiveElement(Iterator<Node> it, Node child)
- {
- it.remove();
- child.dispose();
- }
-
- protected void elementAdded(Object element, Object parent)
- {
- }
-
- protected void elementRemoved(Object element, Object parent)
- {
- }
-
- protected Node getRoot()
- {
- return root;
- }
-
- protected Map<Object, Node> getNodes()
- {
- return nodes;
- }
-
- protected Node getNode(Object element)
- {
- Node node = root;
- if (element != getInput())
- {
- node = nodes.get(element);
- }
-
- if (node == null)
- {
- throw new IllegalStateException("No node for " + element); //$NON-NLS-1$
- }
-
- return node;
- }
-
- protected Node createNode(Node parent, Object element)
- {
- if (element instanceof IContainer<?>)
- {
- return createContaineNode(parent, element);
- }
-
- return createLeafNode(parent, element);
- }
-
- protected LeafNode createLeafNode(Node parent, Object element)
- {
- return new LeafNode(parent, element);
- }
-
- @SuppressWarnings("unchecked")
- protected ContainerNode createContaineNode(Node parent, Object element)
- {
- return new ContainerNode(parent, (IContainer<Object>)element);
- }
-
- protected void addNode(Object element, Node node)
- {
- nodes.put(element, node);
- }
-
- protected Node removeNode(Object element)
- {
- return nodes.remove(element);
- }
-
- protected boolean filterRootElement(Object element)
- {
- if (rootElementFilter != null)
- {
- return rootElementFilter.filter(element);
- }
-
- return true;
- }
-
- /**
- * @since 3.1
- */
- protected void executeRunnable(Runnable runnable)
- {
- Thread thread = new Thread(runnable);
- thread.setDaemon(true);
- thread.start();
- }
-
- /**
- * @since 3.1
- */
- protected boolean isSlow(IContainer<Object> container)
- {
- return container instanceof ISlow;
- }
-
- /**
- * @since 3.1
- */
- protected String getSlowText(IContainer<Object> container)
- {
- return "Pending...";
- }
-
- /**
- * @since 3.1
- */
- protected String getErrorText(IContainer<Object> container)
- {
- return "Error";
- }
-
- @Override
- protected void fillContextMenu(IMenuManager manager, ITreeSelection selection)
- {
- super.fillContextMenu(manager, selection);
- if (selection.size() == 1)
- {
- Object element = selection.getFirstElement();
- if (element instanceof ContainerItemProvider.ErrorElement)
- {
- manager.add(new Action("Open Error Log")
- {
- @Override
- public void run()
- {
- try
- {
- UIUtil.getActiveWorkbenchPage().showView(UIUtil.ERROR_LOG_ID);
- }
- catch (PartInitException ex)
- {
- OM.LOG.error(ex);
- }
- }
- });
- }
- }
- }
-
- @Override
- public Font getFont(Object obj)
- {
- if (obj instanceof ContainerItemProvider.LazyElement)
- {
- return getItalicFont();
- }
-
- return super.getFont(obj);
- }
-
- @Override
- public Color getForeground(Object obj)
- {
- if (obj instanceof ContainerItemProvider.LazyElement)
- {
- return getDisplay().getSystemColor(SWT.COLOR_GRAY);
- }
-
- return super.getForeground(obj);
- }
-
- @Override
- public Image getImage(Object obj)
- {
- if (obj instanceof ContainerItemProvider.LazyElement)
- {
- return SharedIcons.getImage(SharedIcons.OBJ_PENDING);
- }
-
- if (obj instanceof ContainerItemProvider.ErrorElement)
- {
- return SharedIcons.getImage(SharedIcons.OBJ_ERROR);
- }
-
- return super.getImage(obj);
- }
-
- /**
- * @author Eike Stepper
- */
- public interface Node
- {
- public void dispose();
-
- public boolean isDisposed();
-
- public Object getElement();
-
- public Node getParent();
-
- public List<Node> getChildren();
-
- public TreePath getTreePath();
- }
-
- /**
- * @author Eike Stepper
- */
- public abstract class AbstractNode implements Node
- {
- private Node parent;
-
- private boolean disposed;
-
- public AbstractNode(Node parent)
- {
- this.parent = parent;
- }
-
- public void dispose()
- {
- if (!disposed)
- {
- removeNode(getElement());
- parent = null;
- disposed = true;
- }
- }
-
- public boolean isDisposed()
- {
- return disposed;
- }
-
- @Override
- public String toString()
- {
- return MessageFormat.format("{0}[{1}]", getClass().getSimpleName(), getElement()); //$NON-NLS-1$
- }
-
- public final Node getParent()
- {
- checkNotDisposed();
- return parent;
- }
-
- public TreePath getTreePath()
- {
- TreePath parentPath = parent == null ? TreePath.EMPTY : parent.getTreePath();
- return parentPath.createChildPath(getElement());
- }
-
- protected void checkNotDisposed()
- {
- if (disposed)
- {
- throw new IllegalStateException("Node is already disposed of"); //$NON-NLS-1$
- }
- }
-
- protected Node addChild(Collection<Node> children, Object element)
- {
- if (nodes.containsKey(element))
- {
- return null;
- }
-
- if (this != root || filterRootElement(element))
- {
- Node node = createNode(this, element);
- if (node != null)
- {
- addNode(element, node);
- children.add(node);
- return node;
- }
- }
-
- return null;
- }
- }
-
- /**
- * @author Eike Stepper
- */
- public abstract class AbstractContainerNode extends AbstractNode
- {
- private List<Node> children;
-
- protected IListener containerListener = new ContainerEventAdapter<Object>()
- {
- @Override
- protected void onAdded(IContainer<Object> container, Object element)
- {
- AbstractContainerNode.this.onAdded(container, element);
- }
-
- @Override
- protected void onRemoved(IContainer<Object> container, Object element)
- {
- AbstractContainerNode.this.onRemoved(container, element);
- }
-
- @Override
- protected void notifyOtherEvent(IEvent event)
- {
- updateLabels(event.getSource());
- }
- };
-
- public AbstractContainerNode(Node parent)
- {
- super(parent);
- }
-
- @Override
- public void dispose()
- {
- if (!isDisposed())
- {
- if (children != null)
- {
- for (Node child : children)
- {
- child.dispose();
- }
-
- children.clear();
- children = null;
- containerListener = null;
- }
-
- super.dispose();
- }
- }
-
- public final List<Node> getChildren()
- {
- checkNotDisposed();
- if (children == null)
- {
- children = createChildren();
- }
-
- return children;
- }
-
- @SuppressWarnings("unchecked")
- public IContainer<Object> getContainer()
- {
- return (IContainer<Object>)getElement();
- }
-
- protected List<Node> createChildren()
- {
- final List<Node> children = new ArrayList<Node>();
- final IContainer<Object> container = getContainer();
-
- if (isSlow(container))
- {
- final LazyElement lazyElement = new LazyElement(container);
- addChild(children, lazyElement);
-
- Runnable runnable = new Runnable()
- {
- public void run()
- {
- try
- {
- fillChildren(children, container);
- }
- catch (Exception ex)
- {
- OM.LOG.error(ex);
- addChild(children, new ErrorElement(container));
- }
- finally
- {
- Node node = removeNode(lazyElement);
- children.remove(node);
- refreshElement(container, false);
- }
- }
- };
-
- executeRunnable(runnable);
- }
- else
- {
- fillChildren(children, container);
- }
-
- container.addListener(containerListener);
- return children;
- }
-
- /**
- * @since 3.1
- */
- protected void fillChildren(List<Node> children, IContainer<Object> container)
- {
- Object[] elements = container.getElements();
- for (int i = 0; i < elements.length; i++)
- {
- Object element = elements[i];
- addChild(children, element);
- }
- }
-
- protected void onAdded(IContainer<Object> container, Object element)
- {
- Node node = addChild(getChildren(), element);
- if (node != null)
- {
- refreshElement(container, true);
- revealElement(element);
- elementAdded(element, container);
- }
- }
-
- protected void onRemoved(IContainer<Object> container, Object element)
- {
- Node node = removeNode(element);
- if (node != null)
- {
- getChildren().remove(node);
- elementRemoved(element, container);
-
- Object rootElement = root.getElement();
- Object refreshElement = container == rootElement ? null : container;
- refreshElement(refreshElement, true);
- node.dispose();
- }
- }
- }
-
- /**
- * @author Eike Stepper
- */
- public class ContainerNode extends AbstractContainerNode
- {
- private IContainer<Object> container;
-
- public ContainerNode(Node parent, IContainer<Object> container)
- {
- super(parent);
- this.container = container;
- if (container == null)
- {
- throw new IllegalArgumentException("container == null"); //$NON-NLS-1$
- }
- }
-
- @Override
- public void dispose()
- {
- if (!isDisposed())
- {
- container.removeListener(containerListener);
- container = null;
- super.dispose();
- }
- }
-
- public Object getElement()
- {
- return container;
- }
- }
-
- /**
- * @author Eike Stepper
- */
- public class LeafNode extends AbstractNode implements IListener
- {
- private Object element;
-
- public LeafNode(Node parent, Object element)
- {
- super(parent);
- this.element = element;
- EventUtil.addListener(element, this);
- }
-
- @Override
- public void dispose()
- {
- if (!isDisposed())
- {
- EventUtil.removeListener(element, this);
- element = null;
- super.dispose();
- }
- }
-
- public Object getElement()
- {
- checkNotDisposed();
- return element;
- }
-
- public List<Node> getChildren()
- {
- checkNotDisposed();
- return Collections.emptyList();
- }
-
- public void notifyEvent(IEvent event)
- {
- updateLabels(event.getSource());
- }
- }
-
- /**
- * @author Eike Stepper
- * @since 3.1
- */
- public class LazyElement
- {
- private IContainer<Object> container;
-
- public LazyElement(IContainer<Object> container)
- {
- this.container = container;
- }
-
- public IContainer<Object> getContainer()
- {
- return container;
- }
-
- @Override
- public String toString()
- {
- return getSlowText(container);
- }
- }
-
- /**
- * @author Eike Stepper
- * @since 3.1
- */
- public class ErrorElement
- {
- private IContainer<Object> container;
-
- public ErrorElement(IContainer<Object> container)
- {
- this.container = container;
- }
-
- public IContainer<Object> getContainer()
- {
- return container;
- }
-
- @Override
- public String toString()
- {
- return getErrorText(container);
- }
- }
-}
+/*
+ * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ */
+package org.eclipse.net4j.util.ui.views;
+
+import org.eclipse.net4j.internal.util.bundle.OM;
+import org.eclipse.net4j.ui.shared.SharedIcons;
+import org.eclipse.net4j.util.container.ContainerEventAdapter;
+import org.eclipse.net4j.util.container.IContainer;
+import org.eclipse.net4j.util.container.ISlow;
+import org.eclipse.net4j.util.event.EventUtil;
+import org.eclipse.net4j.util.event.IEvent;
+import org.eclipse.net4j.util.event.IListener;
+import org.eclipse.net4j.util.lifecycle.LifecycleState;
+import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
+import org.eclipse.net4j.util.ui.UIUtil;
+
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IMenuManager;
+import org.eclipse.jface.viewers.ITreeSelection;
+import org.eclipse.jface.viewers.TreePath;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.Font;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.ui.PartInitException;
+
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Eike Stepper
+ */
+/**
+ * @author Eike Stepper
+ */
+public class ContainerItemProvider<CONTAINER extends IContainer<Object>> extends ItemProvider<CONTAINER>
+{
+ private Map<Object, Node> nodes = new HashMap<Object, Node>();
+
+ private Node root;
+
+ private IElementFilter rootElementFilter;
+
+ public ContainerItemProvider()
+ {
+ }
+
+ public ContainerItemProvider(IElementFilter rootElementFilter)
+ {
+ this.rootElementFilter = rootElementFilter;
+ }
+
+ public IElementFilter getRootElementFilter()
+ {
+ return rootElementFilter;
+ }
+
+ public Object[] getChildren(Object element)
+ {
+ try
+ {
+ Node node = getNode(element);
+ List<Node> children = node.getChildren();
+ for (Iterator<Node> it = children.iterator(); it.hasNext();)
+ {
+ Node child = it.next();
+ if (child.isDisposed())
+ {
+ it.remove();
+ }
+ else
+ {
+ Object childElement = child.getElement();
+ LifecycleState lifecycleState = LifecycleUtil.getLifecycleState(childElement);
+ if (lifecycleState == LifecycleState.INACTIVE || lifecycleState == LifecycleState.DEACTIVATING)
+ {
+ handleInactiveElement(it, child);
+ }
+ }
+ }
+
+ Object[] result = new Object[children.size()];
+ for (int i = 0; i < result.length; i++)
+ {
+ result[i] = children.get(i).getElement();
+ }
+
+ return result;
+ }
+ catch (RuntimeException ex)
+ {
+ return NO_ELEMENTS;
+ }
+ }
+
+ public Object getParent(Object element)
+ {
+ try
+ {
+ Node node = getNode(element);
+ Node parentNode = node.getParent();
+ return parentNode == null ? null : parentNode.getElement();
+ }
+ catch (RuntimeException ex)
+ {
+ return null;
+ }
+ }
+
+ @Override
+ public void dispose()
+ {
+ super.dispose();
+ }
+
+ @Override
+ protected void connectInput(CONTAINER input)
+ {
+ root = createNode(null, input);
+ if (root != null)
+ {
+ addNode(input, root);
+ }
+ }
+
+ @Override
+ protected void disconnectInput(CONTAINER input)
+ {
+ root.dispose();
+ root = null;
+ nodes.clear();
+ }
+
+ /**
+ * @since 2.0
+ */
+ protected void handleInactiveElement(Iterator<Node> it, Node child)
+ {
+ it.remove();
+ child.dispose();
+ }
+
+ protected void elementAdded(Object element, Object parent)
+ {
+ }
+
+ protected void elementRemoved(Object element, Object parent)
+ {
+ }
+
+ protected Node getRoot()
+ {
+ return root;
+ }
+
+ protected Map<Object, Node> getNodes()
+ {
+ return nodes;
+ }
+
+ protected Node getNode(Object element)
+ {
+ Node node = root;
+ if (element != getInput())
+ {
+ node = nodes.get(element);
+ }
+
+ if (node == null)
+ {
+ throw new IllegalStateException("No node for " + element); //$NON-NLS-1$
+ }
+
+ return node;
+ }
+
+ protected Node createNode(Node parent, Object element)
+ {
+ if (element instanceof IContainer<?>)
+ {
+ return createContaineNode(parent, element);
+ }
+
+ return createLeafNode(parent, element);
+ }
+
+ protected LeafNode createLeafNode(Node parent, Object element)
+ {
+ return new LeafNode(parent, element);
+ }
+
+ @SuppressWarnings("unchecked")
+ protected ContainerNode createContaineNode(Node parent, Object element)
+ {
+ return new ContainerNode(parent, (IContainer<Object>)element);
+ }
+
+ protected void addNode(Object element, Node node)
+ {
+ nodes.put(element, node);
+ }
+
+ protected Node removeNode(Object element)
+ {
+ return nodes.remove(element);
+ }
+
+ protected boolean filterRootElement(Object element)
+ {
+ if (rootElementFilter != null)
+ {
+ return rootElementFilter.filter(element);
+ }
+
+ return true;
+ }
+
+ /**
+ * @since 3.1
+ */
+ protected void executeRunnable(Runnable runnable)
+ {
+ Thread thread = new Thread(runnable);
+ thread.setDaemon(true);
+ thread.start();
+ }
+
+ /**
+ * @since 3.1
+ */
+ protected boolean isSlow(IContainer<Object> container)
+ {
+ return container instanceof ISlow;
+ }
+
+ /**
+ * @since 3.1
+ */
+ protected String getSlowText(IContainer<Object> container)
+ {
+ return "Pending...";
+ }
+
+ /**
+ * @since 3.1
+ */
+ protected String getErrorText(IContainer<Object> container)
+ {
+ return "Error";
+ }
+
+ @Override
+ protected void fillContextMenu(IMenuManager manager, ITreeSelection selection)
+ {
+ super.fillContextMenu(manager, selection);
+ if (selection.size() == 1)
+ {
+ Object element = selection.getFirstElement();
+ if (element instanceof ContainerItemProvider.ErrorElement)
+ {
+ manager.add(new Action("Open Error Log")
+ {
+ @Override
+ public void run()
+ {
+ try
+ {
+ UIUtil.getActiveWorkbenchPage().showView(UIUtil.ERROR_LOG_ID);
+ }
+ catch (PartInitException ex)
+ {
+ OM.LOG.error(ex);
+ }
+ }
+ });
+ }
+ }
+ }
+
+ @Override
+ public Font getFont(Object obj)
+ {
+ if (obj instanceof ContainerItemProvider.LazyElement)
+ {
+ return getItalicFont();
+ }
+
+ return super.getFont(obj);
+ }
+
+ @Override
+ public Color getForeground(Object obj)
+ {
+ if (obj instanceof ContainerItemProvider.LazyElement)
+ {
+ return getDisplay().getSystemColor(SWT.COLOR_GRAY);
+ }
+
+ return super.getForeground(obj);
+ }
+
+ @Override
+ public Image getImage(Object obj)
+ {
+ if (obj instanceof ContainerItemProvider.LazyElement)
+ {
+ return SharedIcons.getImage(SharedIcons.OBJ_PENDING);
+ }
+
+ if (obj instanceof ContainerItemProvider.ErrorElement)
+ {
+ return SharedIcons.getImage(SharedIcons.OBJ_ERROR);
+ }
+
+ return super.getImage(obj);
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ public interface Node
+ {
+ public void dispose();
+
+ public boolean isDisposed();
+
+ public Object getElement();
+
+ public Node getParent();
+
+ public List<Node> getChildren();
+
+ public TreePath getTreePath();
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ public abstract class AbstractNode implements Node
+ {
+ private Node parent;
+
+ private boolean disposed;
+
+ public AbstractNode(Node parent)
+ {
+ this.parent = parent;
+ }
+
+ public void dispose()
+ {
+ if (!disposed)
+ {
+ removeNode(getElement());
+ parent = null;
+ disposed = true;
+ }
+ }
+
+ public boolean isDisposed()
+ {
+ return disposed;
+ }
+
+ @Override
+ public String toString()
+ {
+ return MessageFormat.format("{0}[{1}]", getClass().getSimpleName(), getElement()); //$NON-NLS-1$
+ }
+
+ public final Node getParent()
+ {
+ checkNotDisposed();
+ return parent;
+ }
+
+ public TreePath getTreePath()
+ {
+ TreePath parentPath = parent == null ? TreePath.EMPTY : parent.getTreePath();
+ return parentPath.createChildPath(getElement());
+ }
+
+ protected void checkNotDisposed()
+ {
+ if (disposed)
+ {
+ throw new IllegalStateException("Node is already disposed of"); //$NON-NLS-1$
+ }
+ }
+
+ protected Node addChild(Collection<Node> children, Object element)
+ {
+ if (nodes.containsKey(element))
+ {
+ return null;
+ }
+
+ if (this != root || filterRootElement(element))
+ {
+ Node node = createNode(this, element);
+ if (node != null)
+ {
+ addNode(element, node);
+ children.add(node);
+ return node;
+ }
+ }
+
+ return null;
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ public abstract class AbstractContainerNode extends AbstractNode
+ {
+ private List<Node> children;
+
+ protected IListener containerListener = new ContainerEventAdapter<Object>()
+ {
+ @Override
+ protected void onAdded(IContainer<Object> container, Object element)
+ {
+ AbstractContainerNode.this.onAdded(container, element);
+ }
+
+ @Override
+ protected void onRemoved(IContainer<Object> container, Object element)
+ {
+ AbstractContainerNode.this.onRemoved(container, element);
+ }
+
+ @Override
+ protected void notifyOtherEvent(IEvent event)
+ {
+ updateLabels(event.getSource());
+ }
+ };
+
+ public AbstractContainerNode(Node parent)
+ {
+ super(parent);
+ }
+
+ @Override
+ public void dispose()
+ {
+ if (!isDisposed())
+ {
+ if (children != null)
+ {
+ for (Node child : children)
+ {
+ child.dispose();
+ }
+
+ children.clear();
+ children = null;
+ containerListener = null;
+ }
+
+ super.dispose();
+ }
+ }
+
+ public final List<Node> getChildren()
+ {
+ checkNotDisposed();
+ if (children == null)
+ {
+ children = createChildren();
+ }
+
+ return children;
+ }
+
+ @SuppressWarnings("unchecked")
+ public IContainer<Object> getContainer()
+ {
+ return (IContainer<Object>)getElement();
+ }
+
+ protected List<Node> createChildren()
+ {
+ final List<Node> children = new ArrayList<Node>();
+ final IContainer<Object> container = getContainer();
+
+ if (isSlow(container))
+ {
+ final LazyElement lazyElement = new LazyElement(container);
+ addChild(children, lazyElement);
+
+ Runnable runnable = new Runnable()
+ {
+ public void run()
+ {
+ try
+ {
+ fillChildren(children, container);
+ }
+ catch (Exception ex)
+ {
+ OM.LOG.error(ex);
+ addChild(children, new ErrorElement(container));
+ }
+ finally
+ {
+ Node node = removeNode(lazyElement);
+ children.remove(node);
+ refreshElement(container, false);
+ }
+ }
+ };
+
+ executeRunnable(runnable);
+ }
+ else
+ {
+ fillChildren(children, container);
+ }
+
+ container.addListener(containerListener);
+ return children;
+ }
+
+ /**
+ * @since 3.1
+ */
+ protected void fillChildren(List<Node> children, IContainer<Object> container)
+ {
+ Object[] elements = container.getElements();
+ for (int i = 0; i < elements.length; i++)
+ {
+ Object element = elements[i];
+ addChild(children, element);
+ }
+ }
+
+ protected void onAdded(IContainer<Object> container, Object element)
+ {
+ Node node = addChild(getChildren(), element);
+ if (node != null)
+ {
+ refreshElement(container, true);
+ revealElement(element);
+ elementAdded(element, container);
+ }
+ }
+
+ protected void onRemoved(IContainer<Object> container, Object element)
+ {
+ Node node = removeNode(element);
+ if (node != null)
+ {
+ getChildren().remove(node);
+ elementRemoved(element, container);
+
+ Object rootElement = root.getElement();
+ Object refreshElement = container == rootElement ? null : container;
+ refreshElement(refreshElement, true);
+ node.dispose();
+ }
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ public class ContainerNode extends AbstractContainerNode
+ {
+ private IContainer<Object> container;
+
+ public ContainerNode(Node parent, IContainer<Object> container)
+ {
+ super(parent);
+ this.container = container;
+ if (container == null)
+ {
+ throw new IllegalArgumentException("container == null"); //$NON-NLS-1$
+ }
+ }
+
+ @Override
+ public void dispose()
+ {
+ if (!isDisposed())
+ {
+ container.removeListener(containerListener);
+ container = null;
+ super.dispose();
+ }
+ }
+
+ public Object getElement()
+ {
+ return container;
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ public class LeafNode extends AbstractNode implements IListener
+ {
+ private Object element;
+
+ public LeafNode(Node parent, Object element)
+ {
+ super(parent);
+ this.element = element;
+ EventUtil.addListener(element, this);
+ }
+
+ @Override
+ public void dispose()
+ {
+ if (!isDisposed())
+ {
+ EventUtil.removeListener(element, this);
+ element = null;
+ super.dispose();
+ }
+ }
+
+ public Object getElement()
+ {
+ checkNotDisposed();
+ return element;
+ }
+
+ public List<Node> getChildren()
+ {
+ checkNotDisposed();
+ return Collections.emptyList();
+ }
+
+ public void notifyEvent(IEvent event)
+ {
+ updateLabels(event.getSource());
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ * @since 3.1
+ */
+ public class LazyElement
+ {
+ private IContainer<Object> container;
+
+ public LazyElement(IContainer<Object> container)
+ {
+ this.container = container;
+ }
+
+ public IContainer<Object> getContainer()
+ {
+ return container;
+ }
+
+ @Override
+ public String toString()
+ {
+ return getSlowText(container);
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ * @since 3.1
+ */
+ public class ErrorElement
+ {
+ private IContainer<Object> container;
+
+ public ErrorElement(IContainer<Object> container)
+ {
+ this.container = container;
+ }
+
+ public IContainer<Object> getContainer()
+ {
+ return container;
+ }
+
+ @Override
+ public String toString()
+ {
+ return getErrorText(container);
+ }
+ }
+}

Back to the top