001 /* 002 * AwtMenuWrapper 003 * 004 * Copyright (c) 2001, 2002, 2003, 2004 Marco Schmidt. 005 * All rights reserved. 006 */ 007 008 package net.sourceforge.jiu.gui.awt; 009 010 import java.awt.Menu; 011 import java.awt.MenuBar; 012 import java.awt.MenuItem; 013 import java.awt.MenuShortcut; 014 import java.awt.event.ActionListener; 015 import java.awt.event.KeyEvent; 016 import net.sourceforge.jiu.apps.MenuIndexConstants; 017 import net.sourceforge.jiu.apps.MenuWrapper; 018 import net.sourceforge.jiu.apps.OperationProcessor; 019 import net.sourceforge.jiu.apps.StringIndexConstants; 020 import net.sourceforge.jiu.apps.Strings; 021 022 /** 023 * A wrapper around an AWT MenuBar object. 024 * @author Marco Schmidt 025 * @since 0.8.0 026 */ 027 public class AwtMenuWrapper extends MenuWrapper 028 { 029 private ActionListener listener; 030 private MenuItem[] items; 031 private MenuBar menuBar; 032 033 /** 034 * Internally creates a MenuBar object and provides methods to update that 035 * menu bar. 036 * @param strings String resource used to initialize menu items 037 * @param actionListener a listener which will be registered with all menu items 038 */ 039 public AwtMenuWrapper(Strings strings, ActionListener actionListener) 040 { 041 items = new MenuItem[MenuIndexConstants.NUM_CONSTANTS]; 042 listener = actionListener; 043 init(strings); 044 } 045 046 private Menu createMenu(Strings strings, int stringIndex) 047 { 048 String labelText = strings.get(stringIndex); 049 Menu result = new Menu(labelText); 050 return result; 051 } 052 053 private MenuShortcut createMenuShortcut(int menuIndex) 054 { 055 switch(menuIndex) 056 { 057 case(MenuIndexConstants.FILE_OPEN): return new MenuShortcut(KeyEvent.VK_O); 058 case(MenuIndexConstants.FILE_EXIT): return new MenuShortcut(KeyEvent.VK_Q); 059 case(MenuIndexConstants.EDIT_UNDO): return new MenuShortcut(KeyEvent.VK_Z); 060 case(MenuIndexConstants.EDIT_REDO): return new MenuShortcut(KeyEvent.VK_Y); 061 case(MenuIndexConstants.VIEW_ZOOMIN): return new MenuShortcut(KeyEvent.VK_ADD); 062 case(MenuIndexConstants.VIEW_ZOOMOUT): return new MenuShortcut(KeyEvent.VK_SUBTRACT); 063 case(MenuIndexConstants.VIEW_SETORIGINALSIZE): return new MenuShortcut(KeyEvent.VK_SEPARATER); 064 default: return null; 065 } 066 } 067 068 /** 069 * Attempts to find one of the menu items in the internal list. 070 * Returns its index or -1 if it is not one of the items. 071 */ 072 public int findIndex(Object o) 073 { 074 if (o != null && items != null) 075 { 076 for (int i = 0; i < items.length; i++) 077 { 078 if (o == items[i]) 079 { 080 return i; 081 } 082 } 083 } 084 return -1; 085 } 086 087 public MenuBar getMenuBar() 088 { 089 return menuBar; 090 } 091 092 /** 093 * Initializes an object of type MenuBar. 094 */ 095 private void init(Strings strings) 096 { 097 // by default, create all items as MenuItem objects 098 for (int i = 0; i < items.length; i++) 099 { 100 int stringIndex = getStringIndex(i); 101 if (stringIndex == -1) 102 { 103 continue; 104 } 105 String labelText = strings.get(stringIndex); 106 items[i] = new MenuItem(labelText); 107 } 108 menuBar = new MenuBar(); 109 // FILE - SAVE AS 110 Menu fileSaveAsMenu = createMenu(strings, StringIndexConstants.SAVEAS); 111 items[MenuIndexConstants.FILE_SAVEAS] = fileSaveAsMenu; 112 fileSaveAsMenu.add(items[MenuIndexConstants.FILE_SAVEAS_GIF]); 113 fileSaveAsMenu.add(items[MenuIndexConstants.FILE_SAVEAS_PALM]); 114 fileSaveAsMenu.add(items[MenuIndexConstants.FILE_SAVEAS_PBM]); 115 fileSaveAsMenu.add(items[MenuIndexConstants.FILE_SAVEAS_PGM]); 116 fileSaveAsMenu.add(items[MenuIndexConstants.FILE_SAVEAS_PNG]); 117 fileSaveAsMenu.add(items[MenuIndexConstants.FILE_SAVEAS_PPM]); 118 fileSaveAsMenu.add(items[MenuIndexConstants.FILE_SAVEAS_SUNRASTER]); 119 fileSaveAsMenu.add(items[MenuIndexConstants.FILE_SAVEAS_WINDOWSBMP]); 120 // FILE 121 Menu fileMenu = createMenu(strings, StringIndexConstants.FILE); 122 items[MenuIndexConstants.FILE] = fileMenu; 123 fileMenu.add(items[MenuIndexConstants.FILE_OPEN]); 124 fileMenu.add(fileSaveAsMenu); 125 fileMenu.add(items[MenuIndexConstants.FILE_CLOSE]); 126 fileMenu.addSeparator(); 127 fileMenu.add(items[MenuIndexConstants.FILE_EXIT]); 128 menuBar.add(fileMenu); 129 // EDIT 130 Menu editMenu = createMenu(strings, StringIndexConstants.EDIT); 131 items[MenuIndexConstants.EDIT] = editMenu; 132 editMenu.add(items[MenuIndexConstants.EDIT_UNDO]); 133 editMenu.add(items[MenuIndexConstants.EDIT_REDO]); 134 menuBar.add(editMenu); 135 // COLOR - ADJUST 136 Menu colorAdjustMenu = createMenu(strings, StringIndexConstants.ADJUST); 137 items[MenuIndexConstants.COLOR_ADJUST] = colorAdjustMenu; 138 colorAdjustMenu.add(items[MenuIndexConstants.COLOR_ADJUST_BRIGHTNESS]); 139 colorAdjustMenu.add(items[MenuIndexConstants.COLOR_ADJUST_CONTRAST]); 140 colorAdjustMenu.add(items[MenuIndexConstants.COLOR_ADJUST_GAMMA]); 141 colorAdjustMenu.add(items[MenuIndexConstants.COLOR_ADJUST_HUESATURATIONVALUE]); 142 // COLOR - HISTOGRAM 143 Menu colorHistogramMenu = createMenu(strings, StringIndexConstants.HISTOGRAM); 144 items[MenuIndexConstants.COLOR_HISTOGRAM] = colorHistogramMenu; 145 colorHistogramMenu.add(items[MenuIndexConstants.COLOR_HISTOGRAM_COUNTCOLORSUSED]); 146 colorHistogramMenu.add(items[MenuIndexConstants.COLOR_HISTOGRAM_EQUALIZE]); 147 colorHistogramMenu.add(items[MenuIndexConstants.COLOR_HISTOGRAM_NORMALIZE]); 148 colorHistogramMenu.add(items[MenuIndexConstants.COLOR_HISTOGRAM_TEXTUREPROPERTIES]); 149 colorHistogramMenu.add(items[MenuIndexConstants.COLOR_HISTOGRAM_SAVEHISTOGRAMAS]); 150 colorHistogramMenu.add(items[MenuIndexConstants.COLOR_HISTOGRAM_SAVECOOCCURRENCEMATRIXAS]); 151 colorHistogramMenu.add(items[MenuIndexConstants.COLOR_HISTOGRAM_SAVECOOCCURRENCEFREQUENCYMATRIXAS]); 152 // COLOR - PALETTE 153 Menu colorPaletteMenu = createMenu(strings, StringIndexConstants.PALETTE_MENU_ITEM); 154 items[MenuIndexConstants.COLOR_PALETTE] = colorPaletteMenu; 155 colorPaletteMenu.add(items[MenuIndexConstants.COLOR_PALETTE_SAVEAS]); 156 // COLOR - PROMOTE 157 Menu colorPromoteMenu = createMenu(strings, StringIndexConstants.PROMOTE); 158 items[MenuIndexConstants.COLOR_PROMOTE] = colorPromoteMenu; 159 colorPromoteMenu.add(items[MenuIndexConstants.COLOR_PROMOTE_PROMOTETOPALETTED]); 160 colorPromoteMenu.add(items[MenuIndexConstants.COLOR_PROMOTE_PROMOTETOGRAY8]); 161 colorPromoteMenu.add(items[MenuIndexConstants.COLOR_PROMOTE_PROMOTETOGRAY16]); 162 colorPromoteMenu.add(items[MenuIndexConstants.COLOR_PROMOTE_PROMOTETORGB24]); 163 colorPromoteMenu.add(items[MenuIndexConstants.COLOR_PROMOTE_PROMOTETORGB48]); 164 // COLOR - REDUCE 165 Menu colorReduceMenu = createMenu(strings, StringIndexConstants.REDUCE); 166 items[MenuIndexConstants.COLOR_REDUCE] = colorReduceMenu; 167 colorReduceMenu.add(items[MenuIndexConstants.COLOR_REDUCE_REDUCETOBILEVELTHRESHOLD]); 168 colorReduceMenu.add(items[MenuIndexConstants.COLOR_REDUCE_REDUCENUMBEROFSHADESOFGRAY]); 169 colorReduceMenu.add(items[MenuIndexConstants.COLOR_REDUCE_CONVERTTOGRAYSCALE]); 170 colorReduceMenu.add(items[MenuIndexConstants.COLOR_REDUCE_MEDIANCUT]); 171 colorReduceMenu.add(items[MenuIndexConstants.COLOR_REDUCE_OCTREE]); 172 colorReduceMenu.add(items[MenuIndexConstants.COLOR_REDUCE_UNIFORMPALETTE]); 173 colorReduceMenu.add(items[MenuIndexConstants.COLOR_REDUCE_MAPTOARBITRARYPALETTE]); 174 // COLOR 175 Menu colorMenu = createMenu(strings, StringIndexConstants.COLOR); 176 items[MenuIndexConstants.COLOR] = colorMenu; 177 colorMenu.add(colorAdjustMenu); 178 colorMenu.add(colorHistogramMenu); 179 colorMenu.add(colorPaletteMenu); 180 colorMenu.add(colorPromoteMenu); 181 colorMenu.add(colorReduceMenu); 182 colorMenu.add(items[MenuIndexConstants.COLOR_INVERT]); 183 colorMenu.add(items[MenuIndexConstants.COLOR_CONVERTTOMINIMUMCOLORTYPE]); 184 menuBar.add(colorMenu); 185 // TRANSFORMATIONS 186 Menu transformationsMenu = createMenu(strings, StringIndexConstants.TRANSFORMATIONS); 187 items[MenuIndexConstants.TRANSFORMATIONS] = transformationsMenu; 188 transformationsMenu.add(items[MenuIndexConstants.TRANSFORMATIONS_MIRROR]); 189 transformationsMenu.add(items[MenuIndexConstants.TRANSFORMATIONS_FLIP]); 190 transformationsMenu.addSeparator(); 191 transformationsMenu.add(items[MenuIndexConstants.TRANSFORMATIONS_ROTATELEFT90]); 192 transformationsMenu.add(items[MenuIndexConstants.TRANSFORMATIONS_ROTATERIGHT90]); 193 transformationsMenu.add(items[MenuIndexConstants.TRANSFORMATIONS_ROTATE180]); 194 transformationsMenu.addSeparator(); 195 transformationsMenu.add(items[MenuIndexConstants.TRANSFORMATIONS_CROP]); 196 transformationsMenu.add(items[MenuIndexConstants.TRANSFORMATIONS_SCALE]); 197 transformationsMenu.add(items[MenuIndexConstants.TRANSFORMATIONS_SHEAR]); 198 menuBar.add(transformationsMenu); 199 // FILTERS 200 Menu filtersMenu = createMenu(strings, StringIndexConstants.FILTERS); 201 items[MenuIndexConstants.FILTERS] = filtersMenu; 202 menuBar.add(filtersMenu); 203 filtersMenu.add(items[MenuIndexConstants.FILTERS_BLUR]); 204 filtersMenu.add(items[MenuIndexConstants.FILTERS_SHARPEN]); 205 filtersMenu.add(items[MenuIndexConstants.FILTERS_EDGEDETECTION]); 206 filtersMenu.add(items[MenuIndexConstants.FILTERS_EMBOSS]); 207 filtersMenu.add(items[MenuIndexConstants.FILTERS_PSYCHEDELICDISTILLATION]); 208 filtersMenu.add(items[MenuIndexConstants.FILTERS_LITHOGRAPH]); 209 filtersMenu.add(items[MenuIndexConstants.FILTERS_HORIZONTALSOBEL]); 210 filtersMenu.add(items[MenuIndexConstants.FILTERS_VERTICALSOBEL]); 211 filtersMenu.add(items[MenuIndexConstants.FILTERS_HORIZONTALPREWITT]); 212 filtersMenu.add(items[MenuIndexConstants.FILTERS_VERTICALPREWITT]); 213 filtersMenu.addSeparator(); 214 filtersMenu.add(items[MenuIndexConstants.FILTERS_MINIMUM]); 215 filtersMenu.add(items[MenuIndexConstants.FILTERS_MAXIMUM]); 216 filtersMenu.add(items[MenuIndexConstants.FILTERS_MEDIAN]); 217 filtersMenu.add(items[MenuIndexConstants.FILTERS_MEAN]); 218 filtersMenu.add(items[MenuIndexConstants.FILTERS_OIL]); 219 // VIEW 220 Menu viewMenu = createMenu(strings, StringIndexConstants.VIEW); 221 items[MenuIndexConstants.VIEW] = viewMenu; 222 menuBar.add(viewMenu); 223 viewMenu.add(items[MenuIndexConstants.VIEW_ZOOMIN]); 224 viewMenu.add(items[MenuIndexConstants.VIEW_ZOOMOUT]); 225 viewMenu.add(items[MenuIndexConstants.VIEW_SETORIGINALSIZE]); 226 // VIEW - INTERPOLATION TYPE 227 Menu viewInterpolationMenu = createMenu(strings, StringIndexConstants.VIEW_INTERPOLATIONTYPE); 228 items[MenuIndexConstants.VIEW_INTERPOLATIONTYPE] = viewInterpolationMenu; 229 //viewMenu.add(viewInterpolationMenu); 230 231 /*CheckboxGroup checkboxGroup = new CheckboxGroup(); 232 int stringIndex = getStringIndex(MenuIndexConstants.VIEW_INTERPOLATIONTYPE_NEARESTNEIGHBOR); 233 items[MenuIndexConstants.VIEW_INTERPOLATIONTYPE_NEARESTNEIGHBOR] = new CheckboxMenuItem(strings.get(stringIndex), true); 234 stringIndex = getStringIndex(MenuIndexConstants.VIEW_INTERPOLATIONTYPE_BILINEAR); 235 items[MenuIndexConstants.VIEW_INTERPOLATIONTYPE_BILINEAR] = new CheckboxMenuItem(strings.get(stringIndex), false); 236 stringIndex = getStringIndex(MenuIndexConstants.VIEW_INTERPOLATIONTYPE_BICUBIC); 237 items[MenuIndexConstants.VIEW_INTERPOLATIONTYPE_BICUBIC] = new CheckboxMenuItem(strings.get(stringIndex), false);*/ 238 239 viewInterpolationMenu.add(items[MenuIndexConstants.VIEW_INTERPOLATIONTYPE_NEARESTNEIGHBOR]); 240 viewInterpolationMenu.add(items[MenuIndexConstants.VIEW_INTERPOLATIONTYPE_BILINEAR]); 241 viewInterpolationMenu.add(items[MenuIndexConstants.VIEW_INTERPOLATIONTYPE_BICUBIC]); 242 // HELP 243 Menu helpMenu = createMenu(strings, StringIndexConstants.HELP); 244 items[MenuIndexConstants.HELP] = helpMenu; 245 menuBar.add(helpMenu); 246 helpMenu.add(items[MenuIndexConstants.HELP_ABOUT]); 247 helpMenu.add(items[MenuIndexConstants.HELP_SYSTEMINFORMATION]); 248 // add the listener to all items 249 for (int i = 0; i < items.length; i++) 250 { 251 if (items[i] != null) 252 { 253 MenuShortcut shortcut = createMenuShortcut(i); 254 if (shortcut != null) 255 { 256 items[i].setShortcut(shortcut); 257 } 258 items[i].addActionListener(listener); 259 } 260 } 261 } 262 263 public void setEnabled(int index, boolean enabled) 264 { 265 if (index >= 0 && index < items.length && items[index] != null) 266 { 267 items[index].setEnabled(enabled); 268 } 269 } 270 271 public void setLabel(int index, String text) 272 { 273 if (index >= 0 && index < items.length && items[index] != null) 274 { 275 items[index].setLabel(text); 276 } 277 } 278 279 public void updateEnabled(OperationProcessor op) 280 { 281 for (int i = 0; i < items.length; i++) 282 { 283 setEnabled(i, op.isAvailable(i)); 284 } 285 } 286 287 public void updateLabels(Strings strings) 288 { 289 for (int i = 0; i < items.length; i++) 290 { 291 int stringIndex = getStringIndex(i); 292 String text = strings.get(stringIndex); 293 setLabel(i, text); 294 } 295 } 296 }