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.ui.graphics;
016:
017: import org.eclipse.swt.widgets.Table;
018: import org.eclipse.swt.widgets.TableColumn;
019: import org.eclipse.swt.widgets.Tree;
020: import org.eclipse.swt.widgets.TreeColumn;
021:
022: /**
023: * Utility class for dealing with the resizing of table columns.
024: * <p>
025: *
026: * </p>
027: *
028: * @author chorner
029: * @since 1.0.1
030: */
031: public class TableUtils {
032: // Considerations for future modifications:
033: // -- keep the table somewhat stable
034: // -- keep content visible
035: // -- don't annoy the user with chaotic resizing (which overrides what the
036: // user does)
037:
038: /**
039: * The columns are left as is unless they exceed the bounds set by the
040: * TableSettings.
041: */
042: public static final int MODE_LAZY = 1;
043:
044: /**
045: * Columns are automatically sized, and adjusted to fit into the
046: * TableSettings bounds.
047: */
048: public static final int MODE_AUTO = 2;
049:
050: /**
051: * We start off with MODE_AUTO, but switch to MODE_LAZY when the user
052: * modifies a column size (listener required).
053: */
054: public static final int MODE_JUMP = 3;
055:
056: /**
057: * Given a table, this method resizes the columns as specified. Although
058: * automatic column sizing works well under Linux , it
059: * does not work well under Windows and Mac OSX and hence this method is needed (in
060: * particular, an empty column on the right appears under Win).
061: *
062: * @param table
063: * @param settings
064: * the configured settings for the table (column min, max, etc)
065: * @param mode one of {@link #MODE_AUTO}, {@link #MODE_LAZY} , {@link #MODE_JUMP}
066: */
067: public static void resizeColumns(Table table,
068: TableSettings settings, int mode) {
069: int mode2 = mode;
070: // turn off redraw (reduces flicker)
071: table.setRedraw(false);
072:
073: int tableWidth = table.getSize().x;
074: int columnCount = table.getColumnCount();
075: int columnSum = 0;
076:
077: // load/save the current calling mode
078: if (!(mode2 > 0)) {
079: mode2 = MODE_JUMP; // mode wasn't defined; set a default
080: }
081:
082: // oldMode is only set:
083: // a) when this method is called for the first time
084: // b) when the listener detects a user column-resize <-- NOT IMPLEMENTED
085:
086: // MODES:
087: // MODE_JUMP --> MODE_AUTO
088: // until the user manually-resizes, then:
089: // MODE_JUMP --> MODE_LAZY
090:
091: int oldMode = settings.getCurrentMode();
092: if (oldMode == 0) {
093: settings.setCurrentMode(mode2); // store the mode
094: }
095: if (mode2 == MODE_JUMP) { // hey! we'll actually use the oldMode
096: if (oldMode != mode2) {
097: mode2 = oldMode; // oldMode should be MODE_LAZY
098: } else {
099: mode2 = MODE_AUTO;
100: }
101: }
102:
103: // iterate through each column in the array, and set the size as
104: // appropriate
105: for (int i = 0; i < columnCount - 1; i++) {
106: int minWidth = (int) settings.getColumnMin(i);
107: int maxWidth = (int) (settings.getColumnMax(i) * tableWidth);
108: TableColumn column = table.getColumn(i);
109: if (mode2 == MODE_AUTO)
110: column.pack(); // automatically resize the column
111: int width = column.getWidth();
112: // check the values
113: if (width < minWidth) {
114: // ensure the table isn't incredibly small
115: if (!(columnCount * minWidth > tableWidth)) {
116: // column is too small, and the table is a reasonable size,
117: // so resize it
118: column.setWidth(minWidth);
119: }
120: } else if (width > maxWidth) {
121: // too big
122: column.setWidth(maxWidth);
123: }
124: columnSum += width;
125: }
126:
127: // treat the last column a little bit differently (just fill in the
128: // remaining space)
129: TableColumn column = table.getColumn(columnCount - 1);
130: column.setWidth(tableWidth - columnSum
131: - (2 * table.getBorderWidth()));
132:
133: // TODO: verify that 2*borderWidth is the correct adjustment for MacOS
134: // and Linux (Looks good on Win)
135:
136: // turn redraw back on
137: table.setRedraw(true);
138: }
139:
140: public static void resizeColumns(Tree treeTable,
141: TableSettings settings, int mode) {
142: int mode2 = mode;
143: // turn off redraw (reduces flicker)
144: treeTable.setRedraw(false);
145:
146: int tableWidth = treeTable.getSize().x;
147: int columnCount = treeTable.getColumnCount();
148: int columnSum = 0;
149:
150: // load/save the current calling mode
151: if (!(mode2 > 0)) {
152: mode2 = MODE_JUMP; // mode wasn't defined; set a default
153: }
154:
155: // oldMode is only set:
156: // a) when this method is called for the first time
157: // b) when the listener detects a user column-resize <-- NOT IMPLEMENTED
158:
159: // MODES:
160: // MODE_JUMP --> MODE_AUTO
161: // until the user manually-resizes, then:
162: // MODE_JUMP --> MODE_LAZY
163:
164: int oldMode = settings.getCurrentMode();
165: if (oldMode == 0) {
166: settings.setCurrentMode(mode2); // store the mode
167: }
168: if (mode2 == MODE_JUMP) { // hey! we'll actually use the oldMode
169: if (oldMode != mode2) {
170: mode2 = oldMode; // oldMode should be MODE_LAZY
171: } else {
172: mode2 = MODE_AUTO;
173: }
174: }
175:
176: // iterate through each column in the array, and set the size as
177: // appropriate
178: for (int i = 0; i < columnCount - 1; i++) {
179: int minWidth = (int) settings.getColumnMin(i);
180: int maxWidth = (int) (settings.getColumnMax(i) * tableWidth);
181: TreeColumn column = treeTable.getColumn(i);
182: if (mode2 == MODE_AUTO)
183: column.pack(); // automatically resize the column
184: // check the values
185: if (column.getWidth() < minWidth) {
186: // ensure the table isn't incredibly small
187: if (!(columnCount * minWidth > tableWidth)) {
188: // column is too small, and the table is a reasonable size,
189: // so resize it
190: column.setWidth(minWidth);
191: }
192: } else if (column.getWidth() > maxWidth) {
193: // too big
194: column.setWidth(maxWidth);
195: }
196: columnSum += column.getWidth();
197: }
198:
199: // treat the last column a little bit differently (just fill in the
200: // remaining space)
201: TreeColumn column = treeTable.getColumn(columnCount - 1);
202: column.setWidth(tableWidth - columnSum
203: - (2 * treeTable.getBorderWidth()) - 1);
204:
205: // TODO: verify that 2*borderWidth is the correct adjustment for MacOS
206: // and Linux (Looks good on Win)
207:
208: // turn redraw back on
209: treeTable.setRedraw(true);
210: }
211:
212: // FIXME: Implement the listener, if we need it
213: // public static void createListener(Table table) {
214: // table.addControlListener(new ColumnResizeListener());
215: // }
216:
217: // public class ColumnResizeListener implements ControlListener {
218: // public void controlMoved(ControlEvent e) {
219: // }
220: //
221: // public void controlResized(ControlEvent e) {
222: // System.out.println("Resized!:"+e.getSource().toString());
223: // }
224: // }
225: }
|