KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > cheatsheet > simple > details > SimpleCSCommandComboPart


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.ui.editor.cheatsheet.simple.details;
13
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.ISimpleCSCommandKeyListener;
17 import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.NewCommandKeyEvent;
18 import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSCommandManager;
19 import org.eclipse.pde.internal.ui.parts.ComboPart;
20 import org.eclipse.swt.custom.CCombo;
21 import org.eclipse.swt.events.DisposeEvent;
22 import org.eclipse.swt.events.DisposeListener;
23 import org.eclipse.swt.widgets.Combo;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.ui.forms.widgets.FormToolkit;
26
27 /**
28  * SimpleCSCommandComboPart
29  *
30  */

31 public class SimpleCSCommandComboPart extends ComboPart implements
32         ISimpleCSCommandKeyListener, DisposeListener {
33
34     private int fNewCommandKeyIndex;
35
36     private int fComboEntryLimit;
37     
38     /**
39      *
40      */

41     public SimpleCSCommandComboPart() {
42         super();
43         fNewCommandKeyIndex = -1;
44         fComboEntryLimit = -1;
45     }
46     
47     /**
48      * @param listener
49      */

50     public void addDisposeListener(DisposeListener listener) {
51         if (combo == null) {
52             return;
53         } else if (combo instanceof Combo) {
54             ((Combo) combo).addDisposeListener(listener);
55         } else {
56             ((CCombo) combo).addDisposeListener(listener);
57         }
58     }
59     
60     /* (non-Javadoc)
61      * @see org.eclipse.pde.internal.ui.parts.ComboPart#createControl(org.eclipse.swt.widgets.Composite, org.eclipse.ui.forms.widgets.FormToolkit, int)
62      */

63     public void createControl(Composite parent, FormToolkit toolkit, int style) {
64         super.createControl(parent, toolkit, style);
65         // Connect to the global command manager
66
SimpleCSCommandManager.Instance().addCommandKeyListener(this);
67         // Register to be notified when the combo is diposed in order to
68
// disconnect from the global command manager
69
addDisposeListener(this);
70     }
71     
72     /* (non-Javadoc)
73      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.ISimpleCSCommandKeyListener#newCommandKey(org.eclipse.pde.internal.ui.editor.cheatsheet.simple.NewCommandKeyEvent)
74      */

75     public void newCommandKey(NewCommandKeyEvent event) {
76         // Source: Another combo box
77
String JavaDoc key = event.getKey();
78         // Add the new key to the combo if it does not already exist
79
putValueInCombo(key, fNewCommandKeyIndex);
80     }
81
82     /**
83      * @param key
84      */

85     private void putValueInCombo(String JavaDoc key, int index) {
86         // Ensure the key does not already exist in the combo
87
if (indexOf(key) != -1) {
88             return;
89         }
90         // If we are at the combo entry limit, remove the least recent entry
91
// that is not selected
92
if (getItemCount() >= fComboEntryLimit) {
93             removeLeastRecentEntry();
94         }
95         // Add the new key
96
if (index < 0) {
97             // Add at the end
98
add(key);
99         } else {
100             // Add at the specified index
101
add(key, index);
102         }
103     }
104     
105     /**
106      *
107      */

108     private void removeLeastRecentEntry() {
109         // The least recent entry is the last non-selected entry in the
110
// reciever's list
111
int entryCount = getItemCount();
112         // Nothing to do if there is one entry or no entries
113
if (entryCount <= 1) {
114             return;
115         }
116         // There has to be at least two entries
117
int lastEntry = entryCount - 1;
118         // Remove the last entry if it is NOT selected
119
// Important: The entry may be selected for another model object;
120
// since, the details page is static. As a result, removing the last
121
// entry for this model object may remove a selected entry for another
122
// model object. In that case, the entry is re-inserted into the
123
// reciever when the other model object is selected again
124
if (lastEntry != getSelectionIndex()) {
125             remove(lastEntry);
126             return;
127         }
128         // Last entry was selected, try the second last entry
129
int secondlastEntry = lastEntry - 1;
130         remove(secondlastEntry);
131     }
132     
133     /**
134      * @param limit
135      */

136     public void setComboEntryLimit(int limit) {
137         fComboEntryLimit = limit;
138     }
139     
140     /**
141      * @return
142      */

143     public int getComboEntryLimit() {
144         return fComboEntryLimit;
145     }
146     
147     /**
148      * Specify the index to insert the new command key into the combo box
149      * reciever. Applicable to new command keys obtained via new command key
150      * events (Source: other combo boxes).
151      * @param newCommandKeyIndex
152      */

153     public void setNewCommandKeyIndex(int newCommandKeyIndex) {
154         fNewCommandKeyIndex = newCommandKeyIndex;
155     }
156     
157     /**
158      * @return
159      */

160     public int getNewCommandKeyIndex() {
161         return fNewCommandKeyIndex;
162     }
163
164     /* (non-Javadoc)
165      * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
166      */

167     public void widgetDisposed(DisposeEvent e) {
168         // Disconnect from the global command manager
169
SimpleCSCommandManager.Instance().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 JavaDoc iterator = SimpleCSCommandManager.Instance().getKeys().iterator();
178         while (iterator.hasNext()) {
179             String JavaDoc key = (String JavaDoc)iterator.next();
180             add(key);
181         }
182     }
183     
184     /**
185      * @param key
186      * @param value
187      */

188     public void putValue(String JavaDoc key, String JavaDoc value) {
189         putValue(key, value, -1);
190     }
191     
192     /**
193      * @param key
194      * @param value
195      */

196     public void putValue(String JavaDoc key, String JavaDoc value, int index) {
197         // Source: This combo box
198
// Add the new key to the combo if it does not already exist
199
SimpleCSCommandManager manager = SimpleCSCommandManager.Instance();
200         putValueInCombo(key, index);
201         // Store the actual value in the command manager and notify the
202
// other command combo boxes
203
manager.put(key, value);
204     }
205     
206     /**
207      * @param key
208      * @return
209      */

210     public String JavaDoc getValue(String JavaDoc key) {
211         return SimpleCSCommandManager.Instance().get(key);
212     }
213     
214 }
215
Popular Tags