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.util;
029:
030: import java.util.ArrayList;
031:
032: import org.apache.log4j.Logger;
033: import org.eclipse.jface.viewers.TableViewer;
034: import org.eclipse.jface.viewers.TreeViewer;
035: import org.eclipse.swt.widgets.Table;
036: import org.eclipse.swt.widgets.TableColumn;
037: import org.eclipse.swt.widgets.Tree;
038: import org.eclipse.swt.widgets.TreeColumn;
039:
040: /**
041: * Classe de base des gestionnaires de colonnes de tableau ou d'arbres.
042: */
043: public class TableOrTreeColumnsMgr {
044:
045: /** Logger */
046: private static Logger log = Logger
047: .getLogger(TableOrTreeColumnsMgr.class);
048:
049: /** Liste des colonnes */
050: private ArrayList columns = new ArrayList();
051:
052: /**
053: * Configure les colonnes de l'arbre.
054: * @param treeViewer le viewer de l'arbre.
055: */
056: public void configureTree(TreeViewer treeViewer) {
057: log.debug("configureTree");
058: treeViewer.setColumnProperties(getColumnCodes());
059: Tree tree = treeViewer.getTree();
060: Column[] columns = getColumns();
061: for (int i = 0; i < columns.length; i++) {
062: Column column = columns[i];
063: TreeColumn treeColumn = new TreeColumn(tree,
064: column.alignment);
065: treeColumn.setText(column.name);
066: treeColumn.setWidth(column.width);
067: }
068: }
069:
070: /**
071: * Configure les colonnes d'un tableau.
072: * @param tableViewer le viewer du tableau.
073: */
074: public void configureTable(TableViewer tableViewer) {
075: log.debug("configureTable");
076: tableViewer.setColumnProperties(getColumnCodes());
077: Table table = tableViewer.getTable();
078: Column[] columns = getColumns();
079: for (int i = 0; i < columns.length; i++) {
080: Column column = columns[i];
081: TableColumn tableColumn = new TableColumn(table,
082: column.alignment);
083: tableColumn.setText(column.name);
084: tableColumn.setWidth(column.width);
085: }
086: }
087:
088: /**
089: * Ajoute une colonne.
090: * @param columnCode le code de colonne.
091: * @param columnName le nom de la colonne.
092: * @param columnWidth la largeur de la colonne.
093: * @param columnAlignment l'alignement à appliquer à la colonne.
094: */
095: public void addColumn(String columnCode, String columnName,
096: int columnWidth, int columnAlignment) {
097: Column newColumn = new Column();
098: if (columnName == null)
099: throw new NullPointerException("Column name cannot be null");
100: newColumn.code = columnCode;
101: newColumn.name = columnName;
102: newColumn.width = columnWidth;
103: newColumn.alignment = columnAlignment;
104: columns.add(newColumn);
105: }
106:
107: /**
108: * @return le code associé à la colonne dont le N° est spécifié.
109: * @param columnIndex numéro de la colonne.
110: */
111: public String getColumnCode(int columnIndex) {
112: String code = ((Column) columns.get(columnIndex)).code;
113: log.debug("getColumnCode(" + columnIndex + ")=" + code);
114: return code;
115: }
116:
117: /**
118: * @return le numéro associé à la colonne dont le code est spécifié.
119: * @param columnCode code de la colonne.
120: */
121: public int getColumnIndex(String columnCode) {
122: int index = -1;
123: for (int i = 0; i < columns.size() && index < 0; i++) {
124: Column column = (Column) columns.get(i);
125: if (column.code.equals(columnCode))
126: index = i;
127: }
128: log.debug("getColumnIndex(" + columnCode + ")=" + index);
129: return index;
130: }
131:
132: /**
133: * @return la liste des codes des colonnes.
134: */
135: private String[] getColumnCodes() {
136: int n = columns.size();
137: String[] columnNames = new String[n];
138: for (int i = 0; i < n; i++)
139: columnNames[i] = getColumnCode(i);
140: return columnNames;
141: }
142:
143: /**
144: * @return Retourne la liste des colonnes.
145: */
146: private Column[] getColumns() {
147: return (Column[]) columns.toArray(new Column[columns.size()]);
148: }
149:
150: }
151:
152: /**
153: * Bean contenant les attributs dune colonne.
154: */
155: class Column {
156: String code;
157: String name;
158: int width;
159: int alignment;
160: }
|