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 TasksChooserTree extends AbstractTableMgr implements
046: ITreeContentProvider {
047:
048: /** Logger */
049: private static Logger log = Logger
050: .getLogger(TasksChooserTree.class);
051:
052: /** Constantes associées aux colonnes */
053: public static final int NAME_COLUMN_IDX = 0;
054: public static final int CODE_COLUMN_IDX = 1;
055: private static TableOrTreeColumnsMgr treeColsMgr;
056:
057: /** Viewer */
058: private TreeViewer treeViewer;
059:
060: /** Composant parent */
061: private Composite parent;
062:
063: /**
064: * Constructeur par défaut.
065: * @param parentComposite composant parent.
066: */
067: public TasksChooserTree(Composite parentComposite) {
068: // Création du composite parent
069: parent = new Composite(parentComposite, SWT.NONE);
070: parent.setLayout(new GridLayout(1, false));
071:
072: // Arbre tableau
073: final Tree tree = new Tree(parent, SWT.FULL_SELECTION
074: | SWT.BORDER | SWT.HIDE_SELECTION);
075: GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
076: gridData.heightHint = 300;
077: tree.setLayoutData(gridData);
078: tree.setLinesVisible(true);
079: tree.setHeaderVisible(true);
080: tree.setEnabled(true);
081:
082: // Création du viewer
083: treeViewer = new TreeViewer(tree);
084: treeViewer.setContentProvider(this );
085: treeViewer.setLabelProvider(this );
086:
087: // Configuration des colonnes
088: treeColsMgr = new TableOrTreeColumnsMgr();
089: treeColsMgr.addColumn("NAME", "Task name", 200, SWT.LEFT);
090: treeColsMgr.addColumn("CODE", "Task code", 70, SWT.LEFT);
091: treeColsMgr.configureTree(treeViewer);
092:
093: // Création d'une racine fictive
094: treeViewer.setInput(ROOT_NODE);
095: }
096:
097: /* (non-Javadoc)
098: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
099: */
100: public Object[] getElements(Object inputElement) {
101: return getChildren(null);
102: }
103:
104: /* (non-Javadoc)
105: * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
106: */
107: public Object getValue(Object element, String property) {
108: log.debug("ICellModifier.getValue(" + element + ", " + property
109: + ")");
110: Task task = (Task) element;
111: String value = null;
112: int propertyIdx = treeColsMgr.getColumnIndex(property);
113: switch (propertyIdx) {
114: case (NAME_COLUMN_IDX):
115: value = task.getName();
116: break;
117: case (CODE_COLUMN_IDX):
118: value = task.getCode();
119: break;
120: default:
121: throw new Error("Colonne inconnue");
122: }
123: return value;
124: }
125:
126: /* (non-Javadoc)
127: * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
128: */
129: public boolean hasChildren(Object element) {
130: log.debug("ITreeContentProvider.getChildren(" + element + ")");
131: final Task task = (Task) element;
132: return task.getSubTasksCount() > 0;
133: }
134:
135: /* (non-Javadoc)
136: * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
137: */
138: public Object[] getChildren(Object parentElement) {
139: log.debug("ITreeContentProvider.getChildren(" + parentElement
140: + ")");
141: final Task parentTask = (Task) parentElement;
142: SafeRunner safeRunner = new SafeRunner() {
143: public Object runUnsafe() throws Exception {
144: return ModelMgr.getSubtasks(parentTask);
145: }
146: };
147: Object[] result = (Object[]) safeRunner.run(parent.getShell(),
148: new Object[] {});
149: return result;
150: }
151:
152: /* (non-Javadoc)
153: * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
154: */
155: public Object getParent(Object element) {
156: log.debug("ITreeContentProvider.getParent(" + element + ")");
157: final Task task = (Task) element;
158: SafeRunner safeRunner = new SafeRunner() {
159: public Object runUnsafe() throws Exception {
160: Task parentTask = ModelMgr.getParentTask(task);
161: return parentTask == null ? treeViewer.getInput()
162: : parentTask;
163: }
164: };
165: // Exécution du traitement
166: Object result = (Object) safeRunner.run(parent.getShell());
167: return result;
168: }
169:
170: /* (non-Javadoc)
171: * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
172: */
173: public String getColumnText(Object element, int columnIndex) {
174: log.debug("ITableLabelProvider.getColumnText(" + element + ", "
175: + columnIndex + ")");
176: Task task = (Task) element;
177: String text = null;
178: switch (columnIndex) {
179: case NAME_COLUMN_IDX:
180: text = task.getName();
181: break;
182: case CODE_COLUMN_IDX:
183: text = task.getCode();
184: break;
185: default:
186: throw new Error("Colonne inconnue");
187: }
188: return text;
189: }
190:
191: /**
192: * Retourne le viewer associé à l'arbre.
193: * @return le viewer associé à l'arbre.
194: */
195: public TreeViewer getTreeViewer() {
196: return treeViewer;
197: }
198: }
|