001: //This is free software; for terms and warranty disclaimer see ./COPYING.
002:
003: package gnu.jemacs.swt;
004:
005: import java.util.Iterator;
006: import org.eclipse.jface.dialogs.InputDialog;
007: import org.eclipse.swt.SWT;
008: import org.eclipse.swt.events.SelectionEvent;
009: import org.eclipse.swt.events.SelectionListener;
010: import org.eclipse.swt.layout.FillLayout;
011: import org.eclipse.swt.widgets.Menu;
012: import org.eclipse.swt.widgets.MenuItem;
013: import org.eclipse.swt.widgets.Shell;
014: import gnu.jemacs.buffer.Buffer;
015: import gnu.jemacs.buffer.EFrame;
016: import gnu.lists.FString;
017: import gnu.lists.FVector;
018: import gnu.lists.LList;
019: import gnu.lists.Pair;
020:
021: /**
022: * @author Christian Surlykke
023: * 11-07-2004
024: */
025: public class SwtFrame extends EFrame {
026:
027: private Shell shell;
028: private SwtWindow swtWindow;
029: private Menu menubar;
030:
031: public SwtFrame() {
032: super ();
033: }
034:
035: public SwtFrame(Buffer buffer) {
036: this (new SwtWindow(buffer, true));
037: }
038:
039: public SwtFrame(SwtWindow window) {
040: super (window);
041: this .swtWindow = window;
042: shell = SwtHelper.newShell(SwtHelper.getDisplay(),
043: new FillLayout());
044: swtWindow.getReadyToShow(shell, 0);
045: }
046:
047: /**
048: * @see gnu.jemacs.buffer.EFrame#isLive()
049: */
050: public boolean isLive() {
051: // TODO Auto-generated method stub
052: return false;
053: }
054:
055: /**
056: * @see gnu.jemacs.buffer.EFrame#ask(java.lang.String)
057: */
058: public String ask(String prompt) {
059: Shell shell = new Shell();
060: InputDialog inputDialog = new InputDialog(shell,
061: "Jemacs input window", prompt, "", null);
062: inputDialog.open();
063: String result = inputDialog.getValue();
064: inputDialog.close();
065:
066: return result;
067: }
068:
069: public Shell getShell() {
070: return this .shell;
071: }
072:
073: /**
074: * @see gnu.jemacs.buffer.EFrame#setMenu(gnu.lists.LList)
075: */
076: public void setMenu(LList list) {
077: if (menubar != null) {
078: SwtHelper.dispose(menubar);
079: }
080:
081: menubar = SwtHelper.newMenu(shell, SWT.BAR);
082: setMenuHelper(menubar, list);
083: SwtHelper.setMenuBar(shell, menubar);
084: }
085:
086: /**
087: * Heavily inspired from gnu.jemacs.swing.SwingMenu
088: */
089: private void setMenuHelper(Menu parent, LList list) {
090:
091: for (Iterator iter = list.iterator(); iter.hasNext();) {
092: Object o = iter.next();
093: try {
094: if (o == null) {
095: continue;
096: } else if (o instanceof Pair) {
097: FString menuName = (FString) ((Pair) o).car;
098: MenuItem menuItem = SwtHelper.newMenuItem(parent,
099: SWT.CASCADE, menuName.toString(), null);
100: Menu subMenu = SwtHelper.newMenu(menuItem);
101: setMenuHelper(subMenu, (LList) ((Pair) o).cdr);
102: SwtHelper.setMenu(menuItem, subMenu);
103: } else if (o instanceof FVector) {
104: FString menuItemName = (FString) ((FVector) o)
105: .get(0);
106: Object command = ((FVector) o).get(1);
107: SwtHelper.newMenuItem(parent, SWT.DROP_DOWN,
108: menuItemName.toString(),
109: new MenuCommandHandler(command));
110: } else if (o instanceof FString) {
111: SwtHelper.newMenuItem(parent, SWT.SEPARATOR, null,
112: null);
113: }
114: } catch (Exception e) {
115: System.err.println("SwtFrame.setMenu - problem with "
116: + o);
117: }
118: }
119: }
120:
121: class MenuCommandHandler implements SelectionListener {
122: private Object command;
123:
124: public MenuCommandHandler(Object command) {
125: this .command = command;
126: }
127:
128: public void widgetSelected(SelectionEvent e) {
129: selectedWindow.handleCommand(command);
130: }
131:
132: public void widgetDefaultSelected(SelectionEvent e) {
133: selectedWindow.handleCommand(command);
134: }
135:
136: }
137:
138: }
|