1 3 package gnu.jemacs.swt; 4 5 import java.util.Iterator ; 6 import org.eclipse.jface.dialogs.InputDialog; 7 import org.eclipse.swt.SWT; 8 import org.eclipse.swt.events.SelectionEvent; 9 import org.eclipse.swt.events.SelectionListener; 10 import org.eclipse.swt.layout.FillLayout; 11 import org.eclipse.swt.widgets.Menu; 12 import org.eclipse.swt.widgets.MenuItem; 13 import org.eclipse.swt.widgets.Shell; 14 import gnu.jemacs.buffer.Buffer; 15 import gnu.jemacs.buffer.EFrame; 16 import gnu.lists.FString; 17 import gnu.lists.FVector; 18 import gnu.lists.LList; 19 import gnu.lists.Pair; 20 21 25 public class SwtFrame extends EFrame 26 { 27 28 private Shell shell; 29 private SwtWindow swtWindow; 30 private Menu menubar; 31 32 public SwtFrame () 33 { 34 super(); 35 } 36 37 public SwtFrame (Buffer buffer) 38 { 39 this(new SwtWindow(buffer, true)); 40 } 41 42 public SwtFrame (SwtWindow window) 43 { 44 super(window); 45 this.swtWindow = window; 46 shell = SwtHelper.newShell(SwtHelper.getDisplay(), new FillLayout()); 47 swtWindow.getReadyToShow(shell, 0); 48 } 49 50 53 public boolean isLive() 54 { 55 return false; 57 } 58 59 62 public String ask(String prompt) 63 { 64 Shell shell = new Shell(); 65 InputDialog inputDialog = new InputDialog(shell, "Jemacs input window", prompt, "", null); 66 inputDialog.open(); 67 String result = inputDialog.getValue(); 68 inputDialog.close(); 69 70 return result; 71 } 72 73 public Shell getShell() 74 { 75 return this.shell; 76 } 77 78 81 public void setMenu(LList list) 82 { 83 if (menubar != null) 84 { 85 SwtHelper.dispose(menubar); 86 } 87 88 menubar = SwtHelper.newMenu(shell, SWT.BAR ); 89 setMenuHelper(menubar, list); 90 SwtHelper.setMenuBar(shell, menubar); 91 } 92 93 96 private void setMenuHelper(Menu parent, LList list) 97 { 98 99 for (Iterator iter = list.iterator(); iter.hasNext();) 100 { 101 Object o = iter.next(); 102 try 103 { 104 if (o == null) 105 { 106 continue; 107 } 108 else if (o instanceof Pair) 109 { 110 FString menuName = (FString) ((Pair) o).car; 111 MenuItem menuItem = SwtHelper.newMenuItem(parent, SWT.CASCADE, menuName.toString(), null); 112 Menu subMenu = SwtHelper.newMenu(menuItem); 113 setMenuHelper(subMenu, (LList) ((Pair) o).cdr); 114 SwtHelper.setMenu(menuItem, subMenu); 115 } 116 else if (o instanceof FVector) 117 { 118 FString menuItemName = (FString) ((FVector) o).get(0); 119 Object command = ((FVector) o).get(1); 120 SwtHelper.newMenuItem(parent, SWT.DROP_DOWN, menuItemName.toString(), new MenuCommandHandler(command)); 121 } 122 else if (o instanceof FString) 123 { 124 SwtHelper.newMenuItem(parent, SWT.SEPARATOR, null, null); 125 } 126 } 127 catch (Exception e) 128 { 129 System.err.println("SwtFrame.setMenu - problem with " + o); 130 } 131 } 132 } 133 134 class MenuCommandHandler implements SelectionListener 135 { 136 private Object command; 137 138 public MenuCommandHandler(Object command) 139 { 140 this.command = command; 141 } 142 143 public void widgetSelected(SelectionEvent e) 144 { 145 selectedWindow.handleCommand(command); 146 } 147 148 public void widgetDefaultSelected(SelectionEvent e) 149 { 150 selectedWindow.handleCommand(command); 151 } 152 153 } 154 155 } 156 | Popular Tags |