001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.project.ui.summary;
016:
017: import java.util.Collection;
018:
019: import org.eclipse.jface.viewers.CellEditor;
020: import org.eclipse.jface.viewers.ColumnWeightData;
021: import org.eclipse.jface.viewers.ITableColorProvider;
022: import org.eclipse.jface.viewers.ITableLabelProvider;
023: import org.eclipse.jface.viewers.ITreeContentProvider;
024: import org.eclipse.jface.viewers.LabelProvider;
025: import org.eclipse.jface.viewers.TableLayout;
026: import org.eclipse.jface.viewers.TextCellEditor;
027: import org.eclipse.jface.viewers.TreeViewer;
028: import org.eclipse.jface.viewers.Viewer;
029: import org.eclipse.swt.SWT;
030: import org.eclipse.swt.graphics.Color;
031: import org.eclipse.swt.graphics.Image;
032: import org.eclipse.swt.layout.GridData;
033: import org.eclipse.swt.widgets.Composite;
034: import org.eclipse.swt.widgets.Control;
035: import org.eclipse.swt.widgets.Display;
036: import org.eclipse.swt.widgets.Tree;
037: import org.eclipse.swt.widgets.TreeColumn;
038:
039: /**
040: * A control that shows a TableTree summary of the provided {@link SummaryData}
041: * @author Jesse
042: * @since 1.1.0
043: */
044: public class SummaryControl {
045:
046: private static final String VALUE = "VALUE"; //$NON-NLS-1$
047: private Collection<SummaryData> data;
048: private TreeViewer viewer;
049:
050: public SummaryControl(Collection<SummaryData> data) {
051: this .data = data;
052: }
053:
054: public Control createControl(Composite parent) {
055: Tree tree = new Tree(parent, SWT.V_SCROLL | SWT.H_SCROLL);
056: tree.setLinesVisible(true);
057: tree.setHeaderVisible(true);
058: tree
059: .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
060: true));
061: TableLayout tableLayout = new TableLayout();
062: tableLayout.addColumnData(new ColumnWeightData(1, 200));
063: tableLayout.addColumnData(new ColumnWeightData(3, 200));
064: TreeColumn nameColumn = new TreeColumn(tree, SWT.LEFT);
065: nameColumn.setWidth(200);
066: TreeColumn infoColumn = new TreeColumn(tree, SWT.LEFT
067: | SWT.H_SCROLL);
068: viewer = new TreeViewer(tree);
069: viewer.setContentProvider(new SummaryDataProvider());
070: viewer.setLabelProvider(new SummaryLabelProvider());
071: viewer.setInput(data);
072: viewer.setColumnProperties(new String[] { "TITLE", VALUE }); //$NON-NLS-1$
073:
074: infoColumn.pack();
075: setCellEditor(viewer);
076:
077: return tree;
078: }
079:
080: private void setCellEditor(TreeViewer viewer) {
081: TextCellEditor textCellEditor = new TextCellEditor(viewer
082: .getTree());
083: textCellEditor.setValidator(new SummaryCellEditorValidator(
084: data, viewer.getTree()));
085: viewer
086: .setCellEditors(new CellEditor[] { null, textCellEditor });
087: viewer.setCellModifier(new SummaryCellModifier(data));
088: }
089:
090: /**
091: * Refresh an element
092: *
093: * @param element
094: */
095: public void refresh(Object element) {
096: viewer.refresh(element);
097: }
098:
099: /**
100: * Applies edits if an editor is active
101: */
102: public void applyEdit() {
103: viewer.getCellEditors()[1].deactivate();
104: }
105:
106: /**
107: * Cancel edits if an editor is active
108: */
109: public void cancelEdit() {
110: viewer.cancelEditing();
111: }
112:
113: private class SummaryDataProvider implements ITreeContentProvider {
114:
115: public Object[] getChildren(Object parentElement) {
116: if (parentElement == data) {
117: return data.toArray();
118: }
119: if (parentElement instanceof SummaryData) {
120: return ((SummaryData) parentElement).getChildren();
121: }
122: return null;
123: }
124:
125: public Object getParent(Object element) {
126: if (element instanceof SummaryData) {
127: SummaryData parent = ((SummaryData) element)
128: .getParent();
129: if (parent == null)
130: return data;
131: else
132: return parent;
133: }
134: return null;
135: }
136:
137: public boolean hasChildren(Object element) {
138: if (element == data)
139: return true;
140: if (element instanceof SummaryData) {
141: SummaryData[] children = ((SummaryData) element)
142: .getChildren();
143: if (children != null && children.length > 0)
144: return true;
145: }
146: return false;
147: }
148:
149: public Object[] getElements(Object inputElement) {
150: return getChildren(inputElement);
151: }
152:
153: public void dispose() {
154: }
155:
156: public void inputChanged(Viewer viewer, Object oldInput,
157: Object newInput) {
158: }
159:
160: }
161:
162: private static class SummaryLabelProvider extends LabelProvider
163: implements ITableLabelProvider, ITableColorProvider {
164:
165: public Image getColumnImage(Object element, int columnIndex) {
166: return null;
167: }
168:
169: public String getColumnText(Object element, int columnIndex) {
170: if (!(element instanceof SummaryData))
171: return null;
172: SummaryData data = (SummaryData) element;
173: if (columnIndex == 0)
174: return data.getTitle();
175: return data.getInfo();
176: }
177:
178: public Color getBackground(Object element, int columnIndex) {
179: return null;
180: }
181:
182: public Color getForeground(Object element, int columnIndex) {
183:
184: if (columnIndex == 0)
185: return null;
186: SummaryData data = (SummaryData) element;
187: if (data.getModifier().canModify(element, VALUE))
188: return null;
189: return Display.getCurrent().getSystemColor(
190: SWT.COLOR_DARK_GRAY);
191: }
192:
193: }
194: }
|