001: package com.salmonllc.ideTools.eclipse;
002:
003: import org.eclipse.jface.action.IAction;
004: import org.eclipse.jface.preference.IPreferenceStore;
005: import org.eclipse.jface.viewers.ISelection;
006: import org.eclipse.swt.SWT;
007: import org.eclipse.swt.events.SelectionEvent;
008: import org.eclipse.swt.events.SelectionListener;
009: import org.eclipse.swt.widgets.Control;
010: import org.eclipse.swt.widgets.Menu;
011: import org.eclipse.swt.widgets.MenuItem;
012: import org.eclipse.ui.IWorkbenchPage;
013: import org.eclipse.ui.IWorkbenchWindow;
014: import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
015:
016: import com.salmonllc.ideTools.IDETool;
017:
018: /**
019: * @author Administrator
020: *
021: * To change this generated comment edit the template variable "typecomment":
022: * Window>Preferences>Java>Templates.
023: * To enable and disable the creation of type comments go to
024: * Window>Preferences>Java>Code Generation.
025: */
026: public class RunBrowserAction implements
027: IWorkbenchWindowPulldownDelegate2, SelectionListener {
028: private IWorkbenchWindow _win;
029:
030: /**
031: * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
032: */
033: public void dispose() {
034: }
035:
036: /**
037: * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(IWorkbenchWindow)
038: */
039: public void init(IWorkbenchWindow window) {
040: _win = window;
041: }
042:
043: /**
044: * @see org.eclipse.ui.IActionDelegate#run(IAction)
045: */
046: public void run(IAction action) {
047: runIt(null);
048: }
049:
050: private void runIt(String type) {
051: IPreferenceStore pref = SalmonPlugin.getDefault()
052: .getPreferenceStore();
053: boolean askBeforeSaving = pref
054: .getBoolean(SalmonPlugin.PREF_ASK_SAVE_ON_BROWSER_RUN);
055: boolean save = pref
056: .getBoolean(SalmonPlugin.PREF_SAVE_ON_BROWSER_RUN);
057: String servType = SalmonPlugin.getServerType();
058: boolean restart = pref
059: .getBoolean(SalmonPlugin.PREF_RESTART_SERVER_EACH_RUN)
060: && (servType == IDETool.SERVER_TOMCAT40 || servType == IDETool.SERVER_TOMCAT41);
061:
062: if (save) {
063: IWorkbenchPage p = _win.getActivePage();
064: if (!p.saveAllEditors(askBeforeSaving))
065: return;
066: }
067:
068: if (restart) {
069: StartServerAction.startServer(IDETool.SERVER_TOMCAT40,
070: getArgs(type));
071: return;
072: }
073:
074: runBrowser(type);
075: }
076:
077: private void runBrowser(String type) {
078: try {
079: SalmonPlugin.runIDETool(getArgs(type), false);
080: } catch (Exception e) {
081: e.printStackTrace();
082: }
083: }
084:
085: private String[] getArgs(String type) {
086: if (type == null)
087: type = getDefaultBrowser();
088:
089: String progArgs[] = null;
090: if (type == null)
091: progArgs = new String[] { "-RUNBROWSER", "-PROJECT",
092: SalmonPlugin.getDefaultSalmonProject() };
093: else
094: progArgs = new String[] { "-RUNBROWSER", type, "-PROJECT",
095: SalmonPlugin.getDefaultSalmonProject() };
096: return progArgs;
097:
098: }
099:
100: /**
101: * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
102: */
103: public void selectionChanged(IAction action, ISelection selection) {
104: SalmonPlugin.setSelectedProject(selection);
105: }
106:
107: public void widgetSelected(SelectionEvent e) {
108: if (e.getSource() instanceof MenuItem) {
109: String browser = ((MenuItem) e.getSource()).getText();
110: setDefaultBrowser(browser);
111: runIt(browser);
112: }
113: }
114:
115: private void setDefaultBrowser(String browser) {
116: IPreferenceStore p = SalmonPlugin.getDefault()
117: .getPreferenceStore();
118: p.setValue("DEFAULT_BROWSER", browser);
119: }
120:
121: private String getDefaultBrowser() {
122: IPreferenceStore p = SalmonPlugin.getDefault()
123: .getPreferenceStore();
124: return p.getString("DEFAULT_BROWSER");
125: }
126:
127: public void widgetDefaultSelected(SelectionEvent e) {
128: }
129:
130: public Menu getMenu(Control cont) {
131: Menu main = new Menu(cont);
132: loadMenu(main, true);
133: return main;
134: }
135:
136: public Menu getMenu(Menu m) {
137: Menu main = new Menu(m);
138: loadMenu(main, false);
139: return main;
140: }
141:
142: private void loadMenu(Menu m, boolean sel) {
143: int type = sel ? SWT.CHECK : SWT.CASCADE;
144: String[] browsers = SalmonPlugin.getBrowserTypes();
145: String defBrowser = getDefaultBrowser();
146: for (int i = browsers.length - 1; i >= 0; i--) {
147:
148: MenuItem sub = new MenuItem(m, type);
149: if (sel && defBrowser != null
150: && defBrowser.equals(browsers[i]))
151: sub.setSelection(true);
152: sub.setText(browsers[i]);
153: sub.addSelectionListener(this);
154: }
155: }
156:
157: }
|