001: package fr.aliacom.form.swt.ui;
002:
003: import org.apache.log4j.Logger;
004: import org.eclipse.swt.SWT;
005: import org.eclipse.swt.custom.CLabel;
006: import org.eclipse.swt.events.SelectionEvent;
007: import org.eclipse.swt.events.SelectionListener;
008: import org.eclipse.swt.graphics.Image;
009: import org.eclipse.swt.layout.GridData;
010: import org.eclipse.swt.layout.GridLayout;
011: import org.eclipse.swt.widgets.Button;
012: import org.eclipse.swt.widgets.Composite;
013: import org.eclipse.swt.widgets.Control;
014:
015: import fr.aliacom.commands.Command;
016: import fr.aliacom.common.ui.IOutlookBar;
017: import fr.aliacom.common.ui.tree.ITreeNode;
018: import fr.aliacom.common.ui.tree.TreeModel;
019: import fr.aliacom.form.common.events.CommandEvent;
020: import fr.aliacom.form.common.events.ICommandListener;
021: import fr.aliacom.form.swt.events.CommandMouseListener;
022: import fr.aliacom.form.swt.events.HilightMouseMoveListener;
023: import fr.aliacom.form.swt.ui.tree.CommandTreeNode;
024:
025: /**
026: * @author tom
027: *
028: * (C) 2001, 2002 Thomas Cataldo
029: */
030: public class SWTOutlookBar implements IOutlookBar {
031:
032: private TreeModel model;
033: private Composite c;
034: private Composite[] sectionCommands;
035: private static final Logger log = Logger
036: .getLogger(SWTOutlookBar.class);
037:
038: public SWTOutlookBar(Composite parent) {
039: c = new Composite(parent, SWT.NO_BACKGROUND);
040: GridLayout layout = new GridLayout();
041: layout.numColumns = 1;
042: layout.makeColumnsEqualWidth = true;
043: layout.horizontalSpacing = 0;
044: layout.verticalSpacing = 0;
045: layout.marginHeight = 0;
046: layout.marginWidth = 0;
047: c.setLayout(layout);
048: }
049:
050: /**
051: * @see fr.aliacom.common.ui.IOutlookBar#setModel(fr.aliacom.common.ui.tree.TreeModel)
052: */
053: public void setModel(TreeModel model) {
054: if (this .model != null) {
055: model.removeModelListener(this );
056: }
057: this .model = model;
058: model.addModelListener(this );
059: refillBar();
060: }
061:
062: /**
063: * @see fr.aliacom.form.common.IFormComponent#reset()
064: */
065: public void reset() {
066: }
067:
068: /**
069: * @see fr.aliacom.form.common.IFormComponent#setValueBean(java.lang.Object)
070: */
071: public void setValueBean(Object bean) {
072: }
073:
074: /**
075: * @see fr.aliacom.form.common.IFormComponent#getNativeWidget()
076: */
077: public Object getNativeWidget() {
078: return null;
079: }
080:
081: /**
082: * @see fr.aliacom.common.ui.tree.ITreeModelListener#treeChanged()
083: */
084: public void treeChanged() {
085: refillBar();
086: }
087:
088: /**
089: * Method refillBar.
090: */
091: private void refillBar() {
092: // remove all children
093: Control[] ctrls = c.getChildren();
094: for (int i = 0, n = ctrls.length; i < n; i++) {
095: ctrls[i].dispose();
096: }
097:
098: // create buttons and action
099: log.debug("reffilling bar: " + model.getRoot());
100: ITreeNode[] sections = model.getRoot().children();
101: sectionCommands = new Composite[sections.length];
102: Button section;
103: GridData gd;
104: for (int i = 0, n = sections.length; i < n; i++) {
105: log.debug("Adding section " + sections[i].getText());
106: gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
107:
108: section = new Button(c, SWT.PUSH);
109: section.setText(sections[i].getText());
110: section.setLayoutData(gd);
111: section.addSelectionListener(new SectionListener(i));
112: Composite coms = buildSectionCommands(sections[i]);
113: sectionCommands[i] = coms;
114: }
115: expandSection(0);
116: }
117:
118: private void expandSection(int section) {
119: log.debug("Expanding section " + section);
120:
121: for (int i = 0, n = sectionCommands.length; i < n; i++) {
122: GridData gd;
123: if (i == section) {
124: gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL
125: | GridData.VERTICAL_ALIGN_FILL
126: | GridData.GRAB_VERTICAL);
127: } else {
128: gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
129: gd.heightHint = 0;
130: }
131: sectionCommands[i].setLayoutData(gd);
132: }
133: c.layout(true);
134: }
135:
136: /**
137: * Method buildSectionCommands.
138: * @param iTreeNode
139: */
140: private Composite buildSectionCommands(ITreeNode parent) {
141: Composite ret = new Composite(c, SWT.NO_FOCUS);
142: GridLayout aLayout = new GridLayout();
143: aLayout.numColumns = 1;
144: aLayout.makeColumnsEqualWidth = true;
145: aLayout.horizontalSpacing = 0;
146: aLayout.verticalSpacing = 0;
147: aLayout.marginHeight = 0;
148: aLayout.marginWidth = 0;
149: ret.setLayout(aLayout);
150: for (int i = 0, n = parent.getChildCount(); i < n; i++) {
151: ITreeNode node = parent.getChildAt(i);
152: createButton(node, ret);
153: }
154:
155: return ret;
156: }
157:
158: /**
159: * Method createButton.
160: * @param node
161: * @param ret
162: */
163: private void createButton(ITreeNode node, Composite ret) {
164: final CLabel b = new CLabel(ret, SWT.LEFT);
165: GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
166: b.setLayoutData(data);
167: if (node.getIcon() != null) {
168: b.setImage((Image) node.getIcon().getNativeWidget());
169: }
170: b.setText(node.getText());
171: b.setForeground(b.getDisplay().getSystemColor(SWT.COLOR_BLACK));
172: Command com = ((CommandTreeNode) node).getCommand();
173: final CommandMouseListener cml = new CommandMouseListener(com);
174: b.addMouseListener(cml);
175: final HilightMouseMoveListener hml = new HilightMouseMoveListener(
176: b);
177: b.addMouseTrackListener(hml);
178: if (com != null) {
179: com.addCommandListener(new ICommandListener() {
180: public void stateChanged(CommandEvent ce) {
181: switch (ce.getState()) {
182: case Command.STATE_ENABLED:
183: cml.setEnabled(true);
184: hml.setEnabled(true);
185: b.setEnabled(true);
186: b.setForeground(b.getDisplay().getSystemColor(
187: SWT.COLOR_BLACK));
188: break;
189: case Command.STATE_DISABLED:
190: cml.setEnabled(false);
191: hml.setEnabled(false);
192: b.setEnabled(false);
193: b.setForeground(b.getDisplay().getSystemColor(
194: SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
195: break;
196: default:
197: break;
198: }
199: }
200: });
201: }
202: }
203:
204: private class SectionListener implements SelectionListener {
205: private int mySection;
206:
207: public SectionListener(int i) {
208: mySection = i;
209: }
210:
211: public void widgetDefaultSelected(SelectionEvent arg0) {
212: }
213:
214: public void widgetSelected(SelectionEvent arg0) {
215: expandSection(mySection);
216: }
217:
218: }
219:
220: /**
221: * @see fr.aliacom.common.ui.ITree#getModel()
222: */
223: public TreeModel getModel() {
224: return model;
225: }
226:
227: }
|