tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
0
ZUGRIFFE
1275
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Avatar von zerix
    zerix zerix ist offline Hausmeister
    tutorials.de Moderator
    Registriert seit
    May 2005
    Beiträge
    4.335
    Hallo,

    ich weiß, dass Tom sowas schon mal gepostet hat. Aber ich dachte, ich schreib es mal mit dem SAX-Parser und erweitere es ein wenig.
    Da es teilweise Standard ist, dass die meisten Menu-Punkte auch in einer Toolbar enthalten sind, wird hier auch die Möglichkeit gegeben, eine Menupunkte mit in eine Toolbar zu übernehmen. Dazu muss man nur den toolbar-schalter in der XML-File setzen.
    Zusätlich können auch Mnemonics(Unterstrichene Buchstaben im Menu) und Acceleratoren(z.b. STRG+S) gesetzt werden. Allerdings müssen bei den Acceleratoren die englische Namen der Tasten verwendet werden (Strg -> CTRL). Zusätlich hab ich ein Schema-File geschrieben. Gegen dieses File wird die XML-File validiert, um sicherzustellen, dass die XML-File auch richtig aufgebaut ist.
    Schaut es auch einfach mal an.

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    
    import java.awt.BorderLayout;
     
    import javax.swing.JFrame;
    import javax.swing.JToolBar;
     
    import de.tutorials.menu.MenuCtrl;
     
     
    public class MenuExample extends JFrame {
     
        public MenuExample() {
            super("MenuExample");
            setSize(300,300);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            MenuCtrl menuCtrl = new MenuCtrl();
            try {
                menuCtrl.createMenuBar("mymenu.xml");
            } catch (Exception e) {
                e.printStackTrace();
            }
            JToolBar toolbar = menuCtrl.getToolBar();
            toolbar.setFloatable(false);
            setJMenuBar(menuCtrl.getMenuBar());
            add(toolbar,BorderLayout.NORTH);
            
        }
        
        
        public static void main(String[] args) {
            new MenuExample().setVisible(true);
        }
     
    }

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    
    package de.tutorials.menu;
     
    import java.io.File;
    import java.io.FileInputStream;
    import java.util.Properties;
     
    import javax.swing.JMenuBar;
    import javax.swing.JToolBar;
    import javax.xml.XMLConstants;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import javax.xml.validation.Schema;
    import javax.xml.validation.SchemaFactory;
     
    public class MenuCtrl {
     
        private JMenuBar menubar = null;
     
        private JToolBar toolbar = null;
     
        public JMenuBar getMenuBar() {
            return menubar;
        }
     
        public JToolBar getToolBar() {
            return toolbar;
        }
     
        public void createMenuBar(String file) throws Exception {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                    .newSchema(new File("menu.xsd"));
            factory.setValidating(false);
            factory.setNamespaceAware(true);
            factory.setSchema(schema);
            SAXParser parser = factory.newSAXParser();
            SwingMenuDocHandler handler = new SwingMenuDocHandler();
            parser.parse(new File(file), handler);
     
            menubar = handler.getMenuBar();
            toolbar = handler.getToolBar();
     
        }
     
    }

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    
    package de.tutorials.menu;
     
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.HashMap;
     
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JToolBar;
    import javax.swing.KeyStroke;
     
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    import org.xml.sax.helpers.DefaultHandler;
     
    public class SwingMenuDocHandler extends DefaultHandler {
     
        private final String EL_MENU = "menu";
     
        private final String EL_MENU_ITEM = "menuItem";
     
        private final String EL_SEPARATOR = "separator";
     
        private final String EL_ACTION_LISTENER = "actionListener";
     
        private final String ATT_TOOLBAR = "toolbar";
     
        private final String ATT_ACCELERATOR = "accelerator";
     
        private final String ATT_MNEMONIC = "mnemonic";
     
        private final String ATT_ICON = "icon";
     
        private final String ATT_ACTION = "action";
     
        private final String ATT_TEXT = "text";
     
        private JMenuBar bar = null;
     
        private JToolBar toolbar = null;
     
        private JButton toolbutton = null;
     
        private JMenuItem item = null;
     
        private ArrayList<JMenu> menus = new ArrayList<JMenu>();
     
        private HashMap<String, ActionListener> listeners = new HashMap<String, ActionListener>();
     
        private ImageIcon icon = null;
     
        private String text = null;
     
        private String action = null;
     
        private int last = -1;
     
        boolean hasAction = false;
     
        public void endDocument() throws SAXException {
        }
     
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            if (qName.equals(EL_MENU)) {
                if (last > 0) {
                    menus.get(last - 1).add(menus.get(last));
                } else
                    bar.add(menus.get(last));
                menus.remove(last);
                last--;
            } else if (qName.equals(EL_MENU_ITEM)) {
                toolbutton = null;
                item = null;
                icon = null;
                action = null;
            } else if (qName.equals(EL_ACTION_LISTENER)) {
                hasAction = false;
            }
        }
     
        public void startDocument() throws SAXException {
            bar = new JMenuBar();
        }
     
        public void startElement(String uri, String localName, String qName,
                Attributes atts) throws SAXException {
            if (qName.equals(EL_MENU)) {
                String icon = atts.getValue(ATT_ICON);
                JMenu menu = new JMenu(atts.getValue(ATT_TEXT));
                menu.setMnemonic(getKeyEvent(atts.getValue(ATT_MNEMONIC)));
                if(icon != null)
                    menu.setIcon(new ImageIcon(icon));
                menus.add(menu);
                last++;
            } else if (qName.equals(EL_MENU_ITEM)) {
                addMenuItem(atts);
                if (new Boolean(atts.getValue(ATT_TOOLBAR)).booleanValue()) {
                    addToolbarButton(atts);
                }
            } else if (qName.equals(EL_ACTION_LISTENER)) {
                hasAction = true;
     
            } else if (qName.equals(EL_SEPARATOR)) {
                menus.get(last).addSeparator();
            }
        }
     
        public JMenuBar getMenuBar() {
            return bar;
        }
     
        public JToolBar getToolBar() {
            return toolbar;
        }
     
        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            if (hasAction) {
                String text = new String(ch, start, length);
                text = text.trim();
     
                try {
                    ActionListener listener = null;
                    if (listeners.containsKey(text)) {
                        listener = listeners.get(text);
                    } else {
                        listener = (ActionListener) Class.forName(text)
                                .newInstance();
                        listeners.put(text, listener);
                    }
                    
                    item.addActionListener(listener);
                    if (toolbutton != null) {
                        toolbutton.addActionListener(listener);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
     
        @Override
        public void error(SAXParseException e) throws SAXException {
            System.out.println(e.getLineNumber());
            e.printStackTrace();
            throw new SAXException("XML-Datei entspricht nicht dem Schema");
        }
     
        @Override
        public void fatalError(SAXParseException e) throws SAXException {
            throw new SAXException("XML-Datei entspricht nicht dem Schema");
        }
     
        @Override
        public void warning(SAXParseException e) throws SAXException {
            super.warning(e);
        }
     
        private void addMenuItem(Attributes atts) {
            text = atts.getValue(ATT_TEXT);
            String iconPath = atts.getValue(ATT_ICON);
            if (iconPath != null)
                icon = new ImageIcon(iconPath);
            action = atts.getValue(ATT_ACTION);
     
            item = new JMenuItem();
            item.setText(text);
            if (icon != null) {
                item.setIcon(icon);
            }
            item.setActionCommand(action);
            item.setMnemonic(getKeyEvent(atts.getValue(ATT_MNEMONIC)));
            item.setAccelerator(getKeyStroke(atts.getValue(ATT_ACCELERATOR)));
            menus.get(last).add(item);
        }
     
        private void addToolbarButton(Attributes atts) {
            if (toolbar == null) {
                toolbar = new JToolBar();
            }
     
            toolbutton = new JButton();
            if (icon != null) {
                toolbutton.setIcon(icon);
            } else {
                toolbutton.setText(text);
            }
     
            toolbutton.setToolTipText(text);
            toolbutton.setActionCommand(action);
            toolbutton.setMargin(new Insets(0, 0, 0, 0));
            toolbutton.setBorderPainted(false);
            toolbutton.addMouseListener(new MouseAdapter() {
                public void mouseEntered(MouseEvent e) {
                    if (((JButton) e.getSource()).isEnabled()) {
                        ((JButton) e.getSource()).setBorderPainted(true);
                    }
                }
     
                public void mouseExited(MouseEvent e) {
                    if (((JButton) e.getSource()).isEnabled()) {
                        ((JButton) e.getSource()).setBorderPainted(false);
                    }
                }
            });
            toolbar.add(toolbutton);
        }
     
        private int getKeyEvent(String s) {
            if (s != null && !s.equals("")) {
                String keyEvent = "VK_" + s.toUpperCase();
     
                try {
                    Field f = KeyEvent.class.getField(keyEvent);
     
                    return f.getInt(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return KeyEvent.VK_UNDEFINED;
        }
     
        private KeyStroke getKeyStroke(String s) {
            if (s != null && !s.equals("")) {
                int i = s.indexOf('+');
     
                if (i == -1) {
                    i = s.indexOf('-');
                }
     
                if (i == -1) {
                    return KeyStroke.getKeyStroke(getKeyEvent(s),
                            KeyEvent.VK_UNDEFINED);
                }
     
                String s1 = s.substring(i + 1);
                String actionEvent = s.substring(0, i).toUpperCase() + "_MASK";
     
                try {
                    Field f = ActionEvent.class.getField(actionEvent);
     
                    return KeyStroke.getKeyStroke(getKeyEvent(s1), f.getInt(null));
                } catch (Exception e) {
     
                }
            }
     
            return null;
        }
     
    }

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    package de.tutorials.actions;
     
    import java.awt.event.ActionEvent;
     
    import javax.swing.AbstractAction;
     
    public class MenuAction extends AbstractAction {
     
        public void actionPerformed(ActionEvent e) {
            String action = e.getActionCommand();
            if(action.equals("file.save")){
                System.out.println("Save");
            }
            else if(action.equals("file.exit")){
                System.exit(0);
            }
        }
     
    }

    Code xml:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    <?xml version="1.0" encoding="UTF-8"?>
     
    <menubar xmlns="http://www.home.org/menu" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.home.org/menu menu.xsd ">
        <menu text="Datei">
            <menuItem text="Speichern" action="file.save"  icon="disk.png" mnemonic="s" accelerator="ctrl+s" toolbar="true">
                <actionListener>de.tutorials.actions.MenuAction</actionListener>
            </menuItem>
            <separator/>
            <menuItem text="Beenden" action="file.exit" icon="door_in.png">
                <actionListener>de.tutorials.actions.MenuAction</actionListener>
            </menuItem>
        </menu>
    </menubar>

    Das ist das Schemafile (menu.xsd)
    Code xml:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    
    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.home.org/menu"
        xmlns:tns="http://www.home.org/menu" elementFormDefault="qualified">
     
        <attributeGroup name="menuAttributes">
            <attribute name="text" type="tns:estring" use="required"/>
            <attribute name="mnemonic" type="tns:mstring" use="optional"/>
            <attribute name="icon" type="tns:estring" use="optional"/>
        </attributeGroup>
     
        <attributeGroup name="menuItemExtension">
            <attribute name="action" type="tns:estring" use="required"/>
            
            <attribute name="toolbar" type="boolean" use="optional"/>
            <attribute name="accelerator" type="tns:estring" use="optional"/>
        </attributeGroup>
     
        <group name="menuItemMenuGroup">
            <choice>
                <element name="menuItem" type="tns:menuItemType"/>
                <element name="menu" type="tns:menuType"/>
            </choice>
        </group>
        
        <group name="menuItemGroup">
            <sequence>
                <element name="separator" type="tns:separatorType"/>
                <group ref="tns:menuItemMenuGroup" />
            </sequence>
        </group>
     
        <complexType name="menubarType">
            <sequence>
                <element name="menu" type="tns:menuType" minOccurs="1" maxOccurs="unbounded"> </element>
            </sequence>
        </complexType>
     
        <complexType name="menuType">
            <sequence>
                <group ref="tns:menuItemMenuGroup"/>
                <choice minOccurs="0" maxOccurs="unbounded">
                    <group ref="tns:menuItemMenuGroup"/>
                    <group ref="tns:menuItemGroup" minOccurs="0" maxOccurs="unbounded"> </group>
                </choice>
            </sequence>
            <attributeGroup ref="tns:menuAttributes"/>
        </complexType>
     
        <complexType name="menuItemType">
            <sequence>
                <element name="actionListener" type="tns:estring" minOccurs="0" maxOccurs="unbounded" />
            </sequence>
            <attributeGroup ref="tns:menuAttributes"/>
            <attributeGroup ref="tns:menuItemExtension"/>
        </complexType>
     
        <complexType name="separatorType"> </complexType>
     
        <simpleType name="estring">
            <restriction base="string">
                <minLength value="1"/>
            </restriction>
        </simpleType>
        
        <simpleType name="mstring">
            <restriction base="string">
                <minLength value="1"/>
                <maxLength value="1"/>
            </restriction>
        </simpleType>
     
        <element name="menubar" type="tns:menubarType"/>
    </schema>



    Man kann für jeden Menüpunkt eine eigene Action schreiben oder für Gruppen oder alle die gleiche Action nehmen.

    Für Anregungen und Verbesserungsvorschläge bin ich dankbar.

    Die nächsten Tage werde ich das gleiche mal für SWT posten.

    MFG
    zEriX
    Geändert von zerix (27.04.07 um 18:20 Uhr)
     

Ähnliche Themen

  1. li-menu und co
    Von msycho im Forum CSS
    Antworten: 4
    Letzter Beitrag: 25.09.07, 18:34
  2. Menu
    Von csfungamer im Forum Visual Basic 6.0
    Antworten: 2
    Letzter Beitrag: 21.04.06, 22:40
  3. rechte Maustaste->Menu->noch ein Menu
    Von fischmir im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 2
    Letzter Beitrag: 18.07.05, 15:15
  4. Menu****
    Von Guden im Forum Flash Plattform
    Antworten: 1
    Letzter Beitrag: 02.06.03, 23:17
  5. menu [MX]
    Von Merlin732 im Forum Flash Plattform
    Antworten: 2
    Letzter Beitrag: 09.05.03, 20:30