1 package com.calipso.xmleditor; 2 3 import javax.swing.*; 4 import javax.swing.event.MenuListener ; 5 import javax.swing.event.MenuEvent ; 6 import java.awt.event.ActionListener ; 7 import java.awt.event.ActionEvent ; 8 import java.awt.*; 9 import java.util.LinkedList ; 10 import java.util.Iterator ; 11 12 19 public class XmlEditorMenu extends JMenuBar implements ActionListener , MenuListener { 20 21 private static final int LIMIT = 20; 22 private static final String defaultEmpty = "empty..."; 23 24 private JMenuItem mnuItemNewFromXSD; 25 private JMenuItem mnuItemLoadFromXSD; 26 private JMenuItem mnuItemLoadJustXML; 27 private JMenuItem mnuItemLoadXSD; 28 private JMenuItem mnuUnloadXSD; 29 private JMenuItem mnuItemGenerateReportFromSQL; 30 private JMenuItem mnuItemGenerateReportLayout; 31 private JMenu mnuRecentXml; 32 private JMenu mnuRecentXsd; 33 private LinkedList listRecentXml = new LinkedList (); 34 private LinkedList listRecentXsd = new LinkedList (); 35 private XmlEditorUI xmlEditorUI; 36 private JMenuItem mnuItemSave; 37 private JMenuItem mnuItemSaveAs; 38 private JMenuItem mnuItemGenerateReportView; 39 40 44 public XmlEditorMenu(XmlEditorUI ui) { 45 xmlEditorUI = ui; 46 JMenu mnuFile = new JMenu("Archivo"); 47 JMenu mnuNew = new JMenu("Nuevo"); 48 JMenu mnuLoad = new JMenu("Cargar"); 49 JMenu mnuLoadXml = new JMenu("XML"); 50 JMenu mnuCreateXml = new JMenu("Generar XML"); 51 JMenu mnuRecent = new JMenu("Recientes"); 52 mnuRecentXml = new JMenu("Recientes XML"); 53 mnuRecentXsd = new JMenu("Recientes XSD"); 54 mnuRecent.add(mnuRecentXml); 55 mnuRecent.add(mnuRecentXsd); 56 mnuRecentXml.addMenuListener(this); 57 mnuRecentXsd.addMenuListener(this); 58 59 mnuItemNewFromXSD = new JMenuItem("Nuevo desde actual XSD"); 60 mnuItemNewFromXSD.addActionListener(this); 61 mnuNew.add(mnuItemNewFromXSD); 62 63 mnuItemLoadFromXSD = new JMenuItem("Usar xsd"); 64 mnuItemLoadFromXSD.addActionListener(this); 65 mnuLoadXml.add(mnuItemLoadFromXSD); 66 67 mnuItemLoadJustXML = new JMenuItem("No usar xsd"); 68 mnuItemLoadJustXML.addActionListener(this); 69 mnuLoadXml.add(mnuItemLoadJustXML); 70 71 mnuLoad.add(mnuLoadXml); 72 73 mnuItemLoadXSD = new JMenuItem("XSD"); 74 mnuItemLoadXSD.addActionListener(this); 75 mnuLoad.add(mnuItemLoadXSD); 76 77 mnuLoad.add(mnuRecent); 78 79 mnuItemGenerateReportFromSQL = new JMenuItem("Crear Reporte desde SQL"); 80 mnuItemGenerateReportFromSQL.addActionListener(this); 81 mnuCreateXml.add(mnuItemGenerateReportFromSQL); 82 83 mnuItemGenerateReportView = new JMenuItem("Crear ReportView de ReportDefinition"); 84 mnuItemGenerateReportView.addActionListener(this); 85 mnuCreateXml.add(mnuItemGenerateReportView); 86 87 mnuItemGenerateReportLayout = new JMenuItem("Generar ReportLayout"); 88 mnuItemGenerateReportLayout.addActionListener(this); 89 mnuCreateXml.add(mnuItemGenerateReportLayout); 90 mnuCreateXml.setMnemonic("g".charAt(0)); 91 92 mnuItemSave = new JMenuItem("Guardar XML"); 93 mnuItemSave.addActionListener(this); 94 95 mnuItemSaveAs = new JMenuItem("Guardar como XML"); 96 mnuItemSaveAs.addActionListener(this); 97 98 mnuUnloadXSD = new JMenuItem("Descargar xsd"); 99 mnuUnloadXSD.addActionListener(this); 100 101 mnuFile.add(mnuNew); 102 mnuFile.add(mnuLoad); 103 mnuFile.add(mnuUnloadXSD); 104 mnuFile.add(mnuItemSave); 105 mnuFile.add(mnuItemSaveAs); 106 mnuFile.setMnemonic("a".charAt(0)); 107 108 add(mnuFile); 109 add(mnuCreateXml); 110 } 111 112 public void actionPerformed(ActionEvent e) { 113 if (e.getSource() == mnuItemLoadFromXSD) { 114 getXmlEditorUI().loadFromXml(false,""); 115 }else if (e.getSource() == mnuItemLoadJustXML) { 116 getXmlEditorUI().loadFromXml(true,""); 117 }else if (e.getSource() == mnuItemLoadXSD) { 118 getXmlEditorUI().loadFromXsd(); 119 }else if (e.getSource() == mnuItemGenerateReportFromSQL) { 120 getXmlEditorUI().getParametersFromPane(); 121 }else if (e.getSource() == mnuItemGenerateReportView){ 122 getXmlEditorUI().generateReportView(); 123 }else if (e.getSource() == mnuItemGenerateReportLayout) { 124 getXmlEditorUI().generateReportLayout(); 125 }else if (e.getSource() == mnuItemSave){ 126 getXmlEditorUI().save(true); 127 }else if (e.getSource() == mnuUnloadXSD){ 128 getXmlEditorUI().unLoadxsd(); 129 }else if (e.getSource() == mnuItemSaveAs){ 130 getXmlEditorUI().save(false); 131 }else if (isRecentMenu(e.getSource())) { 132 getXmlEditorUI().openRecent(((JMenuItem) e.getSource()).getName()); 133 } 134 } 135 136 public XmlEditorUI getXmlEditorUI() { 137 return xmlEditorUI; 138 } 139 140 public void setXmlEditorUI(XmlEditorUI xmlEditorUI) { 141 this.xmlEditorUI = xmlEditorUI; 142 } 143 144 public void menuSelected(MenuEvent e) { 145 if (e.getSource() == mnuRecentXml) { 146 addMenuItems(mnuRecentXml, listRecentXml); 147 listRecentXml = new LinkedList (); 148 } 149 else 150 if (e.getSource() == mnuRecentXsd) { 151 addMenuItems(mnuRecentXsd, listRecentXsd); 152 listRecentXsd = new LinkedList (); 153 } 154 } 155 156 161 private void addMenuItems(JMenu menu, LinkedList list) { 162 if (list.isEmpty() && menu.getMenuComponents().length == 0) { 163 JMenuItem empty = new JMenuItem(defaultEmpty); 164 empty.setName(defaultEmpty); 165 menu.add(empty); 166 } else { 167 if (!list.isEmpty()) { 168 for (Iterator iterator = list.iterator(); iterator.hasNext();) { 169 String s = (String ) iterator.next(); 170 if(!hasItem(menu, s)){ 171 JMenuItem item = new JMenuItem(getNameFor(s)); 172 item.setName(s); 173 item.addActionListener(this); 174 menu.add(item); 175 } 176 } 177 } 178 if (menu.getMenuComponents().length > 1 && menu.getMenuComponents()[0].getName().equalsIgnoreCase(defaultEmpty)) { 179 menu.remove(0); 180 } 181 } 182 } 183 184 190 private boolean hasItem(JMenu menu, String s) { 191 Component[] components = menu.getMenuComponents(); 192 for(int i=0; i<components.length; i++){ 193 if(components[i].getName()==s){ 194 return true; 195 } 196 } 197 return false; 198 } 199 200 205 private boolean isRecentMenu(Object source) { 206 if (!doIsRecentMenu(mnuRecentXml, source)) { 207 return doIsRecentMenu(mnuRecentXsd, source); 208 } 209 return true; 210 } 211 212 218 private boolean doIsRecentMenu(JMenu menu, Object source) { 219 Component[] array = menu.getMenuComponents(); 220 String name = ((JMenuItem) source).getName(); 221 for (int i = 0; i < array.length; i++) { 222 if (array[i].getName().equalsIgnoreCase(name)) { 223 return true; 224 } 225 } 226 return false; 227 } 228 229 234 private String getNameFor(String s) { 235 int index = Math.max(s.lastIndexOf("\\"), s.lastIndexOf("/")); 236 return s.substring(index + 1).trim(); 237 } 238 239 public void menuDeselected(MenuEvent e) { 240 } 241 242 public void menuCanceled(MenuEvent e) { 243 } 244 245 249 public void addXsdRecentItem(String file) { 250 addItem(listRecentXsd,file); 251 } 252 253 257 public void addXmlRecentItem(String file) { 258 addItem(listRecentXml, file); 259 } 260 261 266 private void addItem(LinkedList list, String path) { 267 if(!list.contains(path)){ 268 if(list.size() < LIMIT){ 269 list.addLast(path); 270 } 271 } 272 } 273 274 } 275 | Popular Tags |