001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.cheatsheet.simple.details;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.ISimpleCSCommandKeyListener;
015: import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.NewCommandKeyEvent;
016: import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSCommandManager;
017: import org.eclipse.pde.internal.ui.parts.ComboPart;
018: import org.eclipse.swt.custom.CCombo;
019: import org.eclipse.swt.events.DisposeEvent;
020: import org.eclipse.swt.events.DisposeListener;
021: import org.eclipse.swt.widgets.Combo;
022: import org.eclipse.swt.widgets.Composite;
023: import org.eclipse.ui.forms.widgets.FormToolkit;
024:
025: /**
026: * SimpleCSCommandComboPart
027: *
028: */
029: public class SimpleCSCommandComboPart extends ComboPart implements
030: ISimpleCSCommandKeyListener, DisposeListener {
031:
032: private int fNewCommandKeyIndex;
033:
034: private int fComboEntryLimit;
035:
036: /**
037: *
038: */
039: public SimpleCSCommandComboPart() {
040: super ();
041: fNewCommandKeyIndex = -1;
042: fComboEntryLimit = -1;
043: }
044:
045: /**
046: * @param listener
047: */
048: public void addDisposeListener(DisposeListener listener) {
049: if (combo == null) {
050: return;
051: } else if (combo instanceof Combo) {
052: ((Combo) combo).addDisposeListener(listener);
053: } else {
054: ((CCombo) combo).addDisposeListener(listener);
055: }
056: }
057:
058: /* (non-Javadoc)
059: * @see org.eclipse.pde.internal.ui.parts.ComboPart#createControl(org.eclipse.swt.widgets.Composite, org.eclipse.ui.forms.widgets.FormToolkit, int)
060: */
061: public void createControl(Composite parent, FormToolkit toolkit,
062: int style) {
063: super .createControl(parent, toolkit, style);
064: // Connect to the global command manager
065: SimpleCSCommandManager.Instance().addCommandKeyListener(this );
066: // Register to be notified when the combo is diposed in order to
067: // disconnect from the global command manager
068: addDisposeListener(this );
069: }
070:
071: /* (non-Javadoc)
072: * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.ISimpleCSCommandKeyListener#newCommandKey(org.eclipse.pde.internal.ui.editor.cheatsheet.simple.NewCommandKeyEvent)
073: */
074: public void newCommandKey(NewCommandKeyEvent event) {
075: // Source: Another combo box
076: String key = event.getKey();
077: // Add the new key to the combo if it does not already exist
078: putValueInCombo(key, fNewCommandKeyIndex);
079: }
080:
081: /**
082: * @param key
083: */
084: private void putValueInCombo(String key, int index) {
085: // Ensure the key does not already exist in the combo
086: if (indexOf(key) != -1) {
087: return;
088: }
089: // If we are at the combo entry limit, remove the least recent entry
090: // that is not selected
091: if (getItemCount() >= fComboEntryLimit) {
092: removeLeastRecentEntry();
093: }
094: // Add the new key
095: if (index < 0) {
096: // Add at the end
097: add(key);
098: } else {
099: // Add at the specified index
100: add(key, index);
101: }
102: }
103:
104: /**
105: *
106: */
107: private void removeLeastRecentEntry() {
108: // The least recent entry is the last non-selected entry in the
109: // reciever's list
110: int entryCount = getItemCount();
111: // Nothing to do if there is one entry or no entries
112: if (entryCount <= 1) {
113: return;
114: }
115: // There has to be at least two entries
116: int lastEntry = entryCount - 1;
117: // Remove the last entry if it is NOT selected
118: // Important: The entry may be selected for another model object;
119: // since, the details page is static. As a result, removing the last
120: // entry for this model object may remove a selected entry for another
121: // model object. In that case, the entry is re-inserted into the
122: // reciever when the other model object is selected again
123: if (lastEntry != getSelectionIndex()) {
124: remove(lastEntry);
125: return;
126: }
127: // Last entry was selected, try the second last entry
128: int secondlastEntry = lastEntry - 1;
129: remove(secondlastEntry);
130: }
131:
132: /**
133: * @param limit
134: */
135: public void setComboEntryLimit(int limit) {
136: fComboEntryLimit = limit;
137: }
138:
139: /**
140: * @return
141: */
142: public int getComboEntryLimit() {
143: return fComboEntryLimit;
144: }
145:
146: /**
147: * Specify the index to insert the new command key into the combo box
148: * reciever. Applicable to new command keys obtained via new command key
149: * events (Source: other combo boxes).
150: * @param newCommandKeyIndex
151: */
152: public void setNewCommandKeyIndex(int newCommandKeyIndex) {
153: fNewCommandKeyIndex = newCommandKeyIndex;
154: }
155:
156: /**
157: * @return
158: */
159: public int getNewCommandKeyIndex() {
160: return fNewCommandKeyIndex;
161: }
162:
163: /* (non-Javadoc)
164: * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
165: */
166: public void widgetDisposed(DisposeEvent e) {
167: // Disconnect from the global command manager
168: SimpleCSCommandManager.Instance()
169: .removeCommandKeyListener(this );
170: }
171:
172: /**
173: *
174: */
175: public void populate() {
176: // Populate the combo with all the values found in the command manager
177: Iterator iterator = SimpleCSCommandManager.Instance().getKeys()
178: .iterator();
179: while (iterator.hasNext()) {
180: String key = (String) iterator.next();
181: add(key);
182: }
183: }
184:
185: /**
186: * @param key
187: * @param value
188: */
189: public void putValue(String key, String value) {
190: putValue(key, value, -1);
191: }
192:
193: /**
194: * @param key
195: * @param value
196: */
197: public void putValue(String key, String value, int index) {
198: // Source: This combo box
199: // Add the new key to the combo if it does not already exist
200: SimpleCSCommandManager manager = SimpleCSCommandManager
201: .Instance();
202: putValueInCombo(key, index);
203: // Store the actual value in the command manager and notify the
204: // other command combo boxes
205: manager.put(key, value);
206: }
207:
208: /**
209: * @param key
210: * @return
211: */
212: public String getValue(String key) {
213: return SimpleCSCommandManager.Instance().get(key);
214: }
215:
216: }
|