001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.dso.views;
006:
007: import org.eclipse.jdt.internal.ui.util.SelectionUtil;
008: import org.eclipse.jface.action.Action;
009: import org.eclipse.jface.action.ActionContributionItem;
010: import org.eclipse.jface.action.IMenuCreator;
011: import org.eclipse.jface.viewers.ISelection;
012: import org.eclipse.jface.viewers.ISelectionProvider;
013: import org.eclipse.swt.widgets.Control;
014: import org.eclipse.swt.widgets.Menu;
015: import org.eclipse.ui.actions.ActionContext;
016:
017: import com.terracottatech.config.LockLevel;
018:
019: public class LockLevelAction extends Action implements IMenuCreator {
020: ConfigViewPart fPart;
021: Action fRead;
022: Action fWrite;
023: Action fConcurrent;
024: Action fSynchronousWrite;
025:
026: LockLevelAction(ConfigViewPart part) {
027: super ("Lock level", AS_DROP_DOWN_MENU);
028: setMenuCreator(this );
029: fPart = part;
030:
031: fRead = new Action("Read", AS_RADIO_BUTTON) {
032: public void run() {
033: if (isChecked()) {
034: LockLevelAction.this .run();
035: }
036: }
037: };
038: fWrite = new Action("Write", AS_RADIO_BUTTON) {
039: public void run() {
040: if (isChecked()) {
041: LockLevelAction.this .run();
042: }
043: }
044: };
045: fConcurrent = new Action("Concurrent", AS_RADIO_BUTTON) {
046: public void run() {
047: if (isChecked()) {
048: LockLevelAction.this .run();
049: }
050: }
051: };
052: fSynchronousWrite = new Action("Synchronous-write",
053: AS_RADIO_BUTTON) {
054: public void run() {
055: if (isChecked()) {
056: LockLevelAction.this .run();
057: }
058: }
059: };
060: }
061:
062: public void run() {
063: fPart.setLockLevel(this );
064: }
065:
066: LockLevel.Enum getLevel() {
067: if (fRead.isChecked()) {
068: return LockLevel.READ;
069: } else if (fWrite.isChecked()) {
070: return LockLevel.WRITE;
071: } else if (fConcurrent.isChecked()) {
072: return LockLevel.CONCURRENT;
073: } else if (fSynchronousWrite.isChecked()) {
074: return LockLevel.SYNCHRONOUS_WRITE;
075: }
076:
077: return LockLevel.WRITE;
078: }
079:
080: public void setContext(ActionContext context) {
081: Object element = SelectionUtil.getSingleElement(getSelection());
082:
083: if (element instanceof LockWrapper) {
084: LockWrapper wrapper = (LockWrapper) element;
085: LockLevel level = wrapper.getLevel();
086:
087: fRead.setChecked(level != null
088: && level.enumValue() == LockLevel.READ);
089: fWrite.setChecked(level == null
090: || level.enumValue() == LockLevel.WRITE);
091: fConcurrent.setChecked(level != null
092: && level.enumValue() == LockLevel.CONCURRENT);
093: fSynchronousWrite
094: .setChecked(level != null
095: && level.enumValue() == LockLevel.SYNCHRONOUS_WRITE);
096: }
097: }
098:
099: public boolean canActionBeAdded() {
100: Object element = SelectionUtil.getSingleElement(getSelection());
101: return element instanceof LockWrapper;
102: }
103:
104: private ISelection getSelection() {
105: ISelectionProvider provider = fPart.getSite()
106: .getSelectionProvider();
107:
108: if (provider != null) {
109: return provider.getSelection();
110: }
111:
112: return null;
113: }
114:
115: public void dispose() {
116: /**/
117: }
118:
119: private void fillMenu(Menu menu) {
120: ActionContributionItem item;
121:
122: item = new ActionContributionItem(fRead);
123: item.fill(menu, -1);
124:
125: item = new ActionContributionItem(fWrite);
126: item.fill(menu, -1);
127:
128: item = new ActionContributionItem(fConcurrent);
129: item.fill(menu, -1);
130:
131: item = new ActionContributionItem(fSynchronousWrite);
132: item.fill(menu, -1);
133: }
134:
135: public Menu getMenu(Control parent) {
136: Menu menu = new Menu(parent);
137: fillMenu(menu);
138: return menu;
139: }
140:
141: public Menu getMenu(Menu parent) {
142: Menu menu = new Menu(parent);
143: fillMenu(menu);
144: return menu;
145: }
146: }
|