001: /*
002: * Copyright (c) 2004-2006, Jean-François Brazeau. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * 2. Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: *
014: * 3. The name of the author may not be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: * IMPLIEDWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
022: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
023: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
025: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
026: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package jfb.tools.activitymgr.ui.dialogs;
029:
030: import jfb.tools.activitymgr.core.ModelMgr;
031: import jfb.tools.activitymgr.core.beans.Task;
032: import jfb.tools.activitymgr.ui.util.SafeRunner;
033: import jfb.tools.activitymgr.ui.util.AbstractTableMgr;
034: import jfb.tools.activitymgr.ui.util.TableOrTreeColumnsMgr;
035:
036: import org.apache.log4j.Logger;
037: import org.eclipse.jface.viewers.ITreeContentProvider;
038: import org.eclipse.jface.viewers.TreeViewer;
039: import org.eclipse.swt.SWT;
040: import org.eclipse.swt.layout.GridData;
041: import org.eclipse.swt.layout.GridLayout;
042: import org.eclipse.swt.widgets.Composite;
043: import org.eclipse.swt.widgets.Tree;
044:
045: public class TaskChooserTree extends AbstractTableMgr implements
046: ITreeContentProvider {
047:
048: /** Logger */
049: private static Logger log = Logger.getLogger(TaskChooserTree.class);
050:
051: /** Constantes associées aux colonnes */
052: public static final int NAME_COLUMN_IDX = 0;
053: public static final int CODE_COLUMN_IDX = 1;
054: private static TableOrTreeColumnsMgr treeColsMgr;
055:
056: /** Viewer */
057: private TreeViewer treeViewer;
058:
059: /** Composant parent */
060: private Composite parent;
061:
062: /**
063: * Constructeur par défaut.
064: * @param parentComposite composant parent.
065: * @param layoutData données du layout.
066: */
067: public TaskChooserTree(Composite parentComposite, Object layoutData) {
068: // Création du composite parent
069: parent = new Composite(parentComposite, SWT.NONE);
070: parent.setLayoutData(layoutData);
071: parent.setLayout(new GridLayout());
072:
073: // Arbre tableau
074: final Tree tree = new Tree(parent, SWT.FULL_SELECTION
075: | SWT.BORDER | SWT.HIDE_SELECTION);
076: GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
077: gridData.heightHint = 300;
078: tree.setLayoutData(gridData);
079: tree.setLinesVisible(true);
080: tree.setHeaderVisible(true);
081: tree.setEnabled(true);
082:
083: // Création du viewer
084: treeViewer = new TreeViewer(tree);
085: treeViewer.setContentProvider(this );
086: treeViewer.setLabelProvider(this );
087:
088: // Configuration des colonnes
089: treeColsMgr = new TableOrTreeColumnsMgr();
090: treeColsMgr.addColumn("NAME", "Task name", 200, SWT.LEFT);
091: treeColsMgr.addColumn("CODE", "Task code", 70, SWT.LEFT);
092: treeColsMgr.configureTree(treeViewer);
093:
094: // Création d'une racine fictive
095: treeViewer.setInput(ROOT_NODE);
096: }
097:
098: /* (non-Javadoc)
099: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
100: */
101: public Object[] getElements(Object inputElement) {
102: return getChildren(null);
103: }
104:
105: /* (non-Javadoc)
106: * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
107: */
108: public Object getValue(Object element, String property) {
109: log.debug("ICellModifier.getValue(" + element + ", " + property
110: + ")");
111: Task task = (Task) element;
112: String value = null;
113: int propertyIdx = treeColsMgr.getColumnIndex(property);
114: switch (propertyIdx) {
115: case (NAME_COLUMN_IDX):
116: value = task.getName();
117: break;
118: case (CODE_COLUMN_IDX):
119: value = task.getCode();
120: break;
121: default:
122: throw new Error("Colonne inconnue");
123: }
124: return value;
125: }
126:
127: /* (non-Javadoc)
128: * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
129: */
130: public boolean hasChildren(Object element) {
131: log.debug("ITreeContentProvider.getChildren(" + element + ")");
132: final Task task = (Task) element;
133: return task.getSubTasksCount() > 0;
134: }
135:
136: /* (non-Javadoc)
137: * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
138: */
139: public Object[] getChildren(Object parentElement) {
140: log.debug("ITreeContentProvider.getChildren(" + parentElement
141: + ")");
142: final Task parentTask = (Task) parentElement;
143: SafeRunner safeRunner = new SafeRunner() {
144: public Object runUnsafe() throws Exception {
145: return ModelMgr.getSubtasks(parentTask);
146: }
147: };
148: Object[] result = (Object[]) safeRunner.run(parent.getShell(),
149: new Object[] {});
150: return result;
151: }
152:
153: /* (non-Javadoc)
154: * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
155: */
156: public Object getParent(Object element) {
157: log.debug("ITreeContentProvider.getParent(" + element + ")");
158: final Task task = (Task) element;
159: SafeRunner safeRunner = new SafeRunner() {
160: public Object runUnsafe() throws Exception {
161: Task parentTask = ModelMgr.getParentTask(task);
162: return parentTask == null ? treeViewer.getInput()
163: : parentTask;
164: }
165: };
166: // Exécution du traitement
167: Object result = (Object) safeRunner.run(parent.getShell());
168: return result;
169: }
170:
171: /* (non-Javadoc)
172: * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
173: */
174: public String getColumnText(Object element, int columnIndex) {
175: log.debug("ITableLabelProvider.getColumnText(" + element + ", "
176: + columnIndex + ")");
177: Task task = (Task) element;
178: String text = null;
179: switch (columnIndex) {
180: case NAME_COLUMN_IDX:
181: text = task.getName();
182: break;
183: case CODE_COLUMN_IDX:
184: text = task.getCode();
185: break;
186: default:
187: throw new Error("Colonne inconnue");
188: }
189: return text;
190: }
191:
192: /**
193: * Retourne le viewer associé à l'arbre.
194: * @return le viewer associé à l'arbre.
195: */
196: public TreeViewer getTreeViewer() {
197: return treeViewer;
198: }
199: }
|