KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > actions > DirectionFilterActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.team.internal.ui.synchronize.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.action.*;
18 import org.eclipse.jface.util.IPropertyChangeListener;
19 import org.eclipse.jface.util.PropertyChangeEvent;
20 import org.eclipse.team.internal.ui.Utils;
21 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration;
22 import org.eclipse.ui.IActionBars;
23 import org.eclipse.ui.actions.ActionGroup;
24
25 /**
26  * This action group provides radio buttons for each possible direction of synchronization information. The
27  * modes created by this action group can be configured. The actions directly set the mode of an
28  * {@link ISynchronizePageConfiguration}.
29  * @since 3.0
30  */

31 public class DirectionFilterActionGroup extends ActionGroup implements IPropertyChangeListener {
32     
33     // The list of created actions
34
private List JavaDoc actions = new ArrayList JavaDoc(3);
35     
36     // The modes
37
private DirectionFilterAction incomingMode;
38     private DirectionFilterAction outgoingMode;
39     private DirectionFilterAction bothMode;
40     private DirectionFilterAction conflictsMode;
41     
42     private ISynchronizePageConfiguration configuration;
43     
44     /**
45      * An action filter for a specific mode.
46      */

47     class DirectionFilterAction extends Action {
48         private int modeId;
49         
50         public DirectionFilterAction(String JavaDoc prefix,String JavaDoc commandId, int modeId) {
51             super("", AS_RADIO_BUTTON); //$NON-NLS-1$
52
this.modeId = modeId;
53             Utils.initAction(this, prefix);
54         }
55         public void run() {
56             if(isChecked()) {
57                 configuration.setMode(modeId);
58             }
59         }
60         public int getModeId() {
61             return modeId;
62         }
63     }
64     
65     /**
66      * Creates a direction filter group with the given supported modes. The
67      * possible values for modes are defined by the {@link ISynchronizePageConfiguration}
68      * interface.
69      *
70      * @param configuration the page configuration
71      */

72     public DirectionFilterActionGroup(ISynchronizePageConfiguration configuration) {
73         this.configuration = configuration;
74         createActions();
75         configuration.addPropertyChangeListener(this);
76         checkMode(configuration.getMode());
77     }
78     
79     /**
80      * Sets up the sync modes and the actions for switching between them.
81      */

82     private void createActions() {
83         // Create the actions
84
int supportedModes = configuration.getSupportedModes();
85         if (supportedModes == 0) return;
86         int currentMode = configuration.getMode();
87         if ((supportedModes & currentMode) == 0) {
88             currentMode = getSupportedMode(supportedModes);
89             if (currentMode == 0) return;
90             configuration.setMode(currentMode);
91         }
92         if((supportedModes & ISynchronizePageConfiguration.INCOMING_MODE) != 0) {
93             incomingMode = new DirectionFilterAction("action.directionFilterIncoming.", "org.eclipse.team.ui.syncview.incomingFilter", ISynchronizePageConfiguration.INCOMING_MODE); //$NON-NLS-1$ //$NON-NLS-2$
94
actions.add(incomingMode);
95             incomingMode.setChecked(currentMode == ISynchronizePageConfiguration.INCOMING_MODE);
96         }
97         
98         if((supportedModes & ISynchronizePageConfiguration.OUTGOING_MODE) != 0) {
99             outgoingMode = new DirectionFilterAction("action.directionFilterOutgoing.", "org.eclipse.team.ui.syncview.outgoingFilter", ISynchronizePageConfiguration.OUTGOING_MODE); //$NON-NLS-1$ //$NON-NLS-2$
100
actions.add(outgoingMode);
101             outgoingMode.setChecked(currentMode == ISynchronizePageConfiguration.OUTGOING_MODE);
102         }
103         
104         if((supportedModes & ISynchronizePageConfiguration.BOTH_MODE) != 0) {
105             bothMode = new DirectionFilterAction("action.directionFilterBoth.", "org.eclipse.team.ui.syncview.bothFilter", ISynchronizePageConfiguration.BOTH_MODE); //$NON-NLS-1$ //$NON-NLS-2$
106
actions.add(bothMode);
107             bothMode.setChecked(currentMode == ISynchronizePageConfiguration.BOTH_MODE);
108         }
109         
110         if((supportedModes & ISynchronizePageConfiguration.CONFLICTING_MODE) != 0) {
111             conflictsMode = new DirectionFilterAction("action.directionFilterConflicts.", "org.eclipse.team.ui.syncview.conflictsFilter", ISynchronizePageConfiguration.CONFLICTING_MODE); //$NON-NLS-1$ //$NON-NLS-2$
112
actions.add(conflictsMode);
113             conflictsMode.setChecked(currentMode == ISynchronizePageConfiguration.CONFLICTING_MODE);
114         }
115     }
116     
117     /**
118      * @param supportedModes
119      * @return the support mode
120      */

121     private int getSupportedMode(int supportedModes) {
122         if((supportedModes & ISynchronizePageConfiguration.INCOMING_MODE) != 0) {
123             return ISynchronizePageConfiguration.INCOMING_MODE;
124         }
125         if((supportedModes & ISynchronizePageConfiguration.OUTGOING_MODE) != 0) {
126             return ISynchronizePageConfiguration.OUTGOING_MODE;
127         }
128         if((supportedModes & ISynchronizePageConfiguration.BOTH_MODE) != 0) {
129             return ISynchronizePageConfiguration.BOTH_MODE;
130         }
131         if((supportedModes & ISynchronizePageConfiguration.CONFLICTING_MODE) != 0) {
132             return ISynchronizePageConfiguration.CONFLICTING_MODE;
133         }
134         return 0;
135     }
136
137     /* (non-Javadoc)
138      * @see org.eclipse.ui.actions.ActionGroup#fillActionBars(org.eclipse.ui.IActionBars)
139      */

140     public void fillActionBars(IActionBars actionBars, String JavaDoc group) {
141         super.fillActionBars(actionBars);
142         IToolBarManager toolBar = actionBars.getToolBarManager();
143         for (Iterator JavaDoc it = actions.iterator(); it.hasNext();) {
144             DirectionFilterAction action = (DirectionFilterAction) it.next();
145             if(group != null) {
146                 toolBar.appendToGroup(group, action);
147             } else {
148                 toolBar.add(action);
149             }
150         }
151     }
152     
153     public void fillToolBar(String JavaDoc groupId, IToolBarManager toolBar) {
154         for (Iterator JavaDoc it = actions.iterator(); it.hasNext();) {
155             DirectionFilterAction action = (DirectionFilterAction) it.next();
156                 toolBar.appendToGroup(groupId, action);
157         }
158     }
159     
160     public void fillMenu(IContributionManager manager) {
161         for (Iterator JavaDoc it = actions.iterator(); it.hasNext();) {
162             DirectionFilterAction action = (DirectionFilterAction) it.next();
163                 manager.add(action);
164         }
165     }
166     
167     private void checkMode(int mode) {
168         for (Iterator JavaDoc it = actions.iterator(); it.hasNext();) {
169             DirectionFilterAction action = (DirectionFilterAction)it.next();
170             if(action.getModeId() == mode) {
171                 action.setChecked(true);
172             } else {
173                 action.setChecked(false);
174             }
175         }
176     }
177     
178     /* (non-Javadoc)
179      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
180      */

181     public void propertyChange(PropertyChangeEvent event) {
182         if(event.getProperty().equals(ISynchronizePageConfiguration.P_MODE)) {
183             Integer JavaDoc mode = (Integer JavaDoc)event.getNewValue();
184             checkMode(mode.intValue());
185         }
186     }
187     
188     /* (non-Javadoc)
189      * @see org.eclipse.ui.actions.ActionGroup#dispose()
190      */

191     public void dispose() {
192         super.dispose();
193     }
194 }
195
Popular Tags