XMLでGUIを定義してみる(超プロトタイプ)-- このノードが保有する属性と値のMapを全操作して取得する -- NamedNodeMap, Node#getAttributes()の使い方

戻る




:::::::::::::: ButtonMenuTest3.java :::::::::::::: import java.util.*; import java.util.regex.*; import java.net.*; import javax.swing.*; import java.awt.event.*; import java.awt.*; /** * $Id: gui-xml.html,v 1.1 2009/06/22 16:12:12 kishi Exp kishi $ */ public class ButtonMenuTest3 extends JFrame implements ActionListener { private JPanel topPanel; private JPanel mainPanel; private JPanel bottomPanel; private JLabel messageLabel; private java.util.Map menuContextMap; /** コマンド名とJButtonを格納したMap */ private java.util.Map menuButtonMap; /** * メニューのコンテキストを記述したXMLを読み込む */ private void setMenuContext() throws Exception { MenuContextRetriever retriever = new MenuContextRetriever( "menu.xml" ); menuContextMap = retriever.getMap(); } /** * コンストラクタ */ public ButtonMenuTest3() throws Exception { // 上部に配置するパネル topPanel = new JPanel(); topPanel.setPreferredSize( new Dimension( 640, 40 ) ); setMenuContext(); Iterator iterator = menuContextMap.keySet().iterator(); menuButtonMap = new java.util.LinkedHashMap(); while ( iterator.hasNext() ) { String command = ( String ) iterator.next(); MenuContext menuContext = ( MenuContext ) menuContextMap.get( command ); String description = menuContext.getDescription(); JButton button = new JButton( description ); topPanel.add( button ); button.setActionCommand( command ); button.addActionListener( this ); // ボタンとコマンドの関連付け menuButtonMap.put( command, button ); } // 中心に配置するパネル mainPanel = new JPanel(); mainPanel.setPreferredSize( new Dimension( 640, 480 ) ); mainPanel.setBackground( Color.cyan ); // 下部に配置するパネル bottomPanel = new JPanel(); bottomPanel.setPreferredSize( new Dimension( 640, 80 ) ); messageLabel = new JLabel(); bottomPanel.add( messageLabel ); //----------------------------------------------------- // 全ての部品をContainerに配置する //----------------------------------------------------- Container container = this.getContentPane(); container.add( topPanel, BorderLayout.NORTH ); container.add( mainPanel, BorderLayout.CENTER ); container.add( bottomPanel, BorderLayout.SOUTH ); } public void actionPerformed( ActionEvent event ) { String command = event.getActionCommand(); System.out.println( "COMMAND: " + command ); messageLabel.setText( "[" + command + "]が選択されました" ); // 選択されたメニューに応じてレンダリングする mainPanel.removeAll(); // 選択されたコマンドからコンテキストを取得 MenuContext menuContext = ( MenuContext ) menuContextMap.get( command ); // 文字列(16進)から整数に変換 Color background = new Color( Integer.parseInt( menuContext.getBackground(), 16 ) ); mainPanel.setBackground( background ); // 表示すべきコンポーネントをパネルに描画する PanelRenderer.render( mainPanel, menuContext ); //===========================================- // 選択したボタンを使用不可にする //===========================================- resetAllButtons(); getJButton( command ).setEnabled( false ); //===========================================- // 再描画 //===========================================- mainPanel.repaint(); } private void resetAllButtons() { Iterator iterator = menuButtonMap.keySet().iterator(); while ( iterator.hasNext() ) { String command = ( String ) iterator.next(); JButton button = ( JButton ) menuButtonMap.get( command ); button.setEnabled( true ); } } private JButton getJButton( String command ) { return ( JButton ) menuButtonMap.get( command ); } /*****************/ /** MAINメソッド */ /*****************/ public static void main( String[] args ) throws Exception { ButtonMenuTest3 viewer = new ButtonMenuTest3(); // 終了処理 viewer.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); viewer.setBounds( 0, 0, 600, 800 ); // タイトルの表示 viewer.setTitle( "XMLで定義したファイルからGUI自動生成" ); viewer.setVisible( true ); viewer.pack(); } } :::::::::::::: MenuContext.java :::::::::::::: import java.util.*; import java.util.regex.*; import java.net.*; /** * $Id: gui-xml.html,v 1.1 2009/06/22 16:12:12 kishi Exp kishi $ * @author KISHI Yasuhiro */ public class MenuContext { private String description; private String background; private java.util.List formComponentList; /** * コンストラクタ */ public MenuContext() { formComponentList = new LinkedList(); } public void setDescription( String description ) { this.description = description; } public String getDescription() { return description; } public void setBackground( String background ) { this.background = background; } public String getBackground() { return background; } public void addFormComponent( Map map ) { formComponentList.add( map ); } public java.util.List getFormComponentList() { return formComponentList; } } :::::::::::::: MenuContextRetriever.java :::::::::::::: import javax.xml.parsers.*; import org.w3c.dom.*; import java.io.*; import java.util.*; /** * $Id: gui-xml.html,v 1.1 2009/06/22 16:12:12 kishi Exp kishi $ * DOMを使ってXML内の全要素の属性、値を取得 * @author KISHI Yasuhiro */ public class MenuContextRetriever { private Map menuContextMap; public MenuContextRetriever( String fileName ) throws Exception { try { // ドキュメントビルダーファクトリを生成 DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); // ドキュメントビルダーを生成 DocumentBuilder builder = dbfactory.newDocumentBuilder(); // パースを実行してDocumentオブジェクトを取得 Document document = builder.parse( new File( fileName ) ); Node node = document.getDocumentElement(); // 情報を取得 retrieve( node ); } catch ( Exception e ) { throw e; } } private void retrieve( Node node ) { // Mapのインスタンス化 menuContextMap = new java.util.LinkedHashMap(); Node childNode = node.getFirstChild(); while ( childNode != null ) { String nodeName = childNode.getNodeName(); if ( "context".equals( nodeName ) ) { System.out.println( "[" + nodeName + "]" ); // メニューコンテキストの生成 MenuContext context = new MenuContext(); // メニューコンテキストが持つプロパティをセットする getContextProperties( childNode, context ); } childNode = childNode.getNextSibling(); } } private void getContextProperties( Node node, MenuContext context ) { Node childNode = node.getFirstChild(); while ( childNode != null ) { String nodeName = childNode.getNodeName(); if ( "command".equals( nodeName ) ) { System.out.print( "\t" + nodeName ); String commandName = childNode.getFirstChild().getNodeValue(); System.out.println( ": " + commandName ); menuContextMap.put( commandName, context ); } if ( "description".equals( nodeName ) ) { System.out.print( "\t" + nodeName ); String description = childNode.getFirstChild().getNodeValue(); System.out.println( ": " + description ); context.setDescription( description ); } if ( "background".equals( nodeName ) ) { System.out.print( "\t" + nodeName ); String background = childNode.getFirstChild().getNodeValue(); System.out.println( ": " + background ); context.setBackground( background ); } if ( "form".equals( nodeName ) ) { System.out.println( "\t" + nodeName + ":" ); // フォーム内を構成するコンポーネントを求める getFormComponents( childNode, context ); } childNode = childNode.getNextSibling(); } } private void getFormComponents( Node node, MenuContext context ) { Node childNode = node.getFirstChild(); int i = 0; while ( childNode != null ) { String nodeName = childNode.getNodeName(); if ( "component".equals( nodeName ) ) { System.out.print( "\t\t" + nodeName + ": " ); Map componentMap = new HashMap(); // このノードが保有する属性と値のMapを全操作して取得する NamedNodeMap attrMap = childNode.getAttributes(); int attrs = attrMap.getLength(); for ( int iAttr = 0; iAttr < attrs; iAttr++ ) { Node attr = attrMap.item( iAttr ); System.out.print( attr.getNodeName() + "=" + attr.getNodeValue() + "\t" ); componentMap.put( attr.getNodeName(), attr.getNodeValue() ); } // コンポーネントの属性・値をコンテキストに追加 context.addFormComponent( componentMap ); System.out.println(); } childNode = childNode.getNextSibling(); } } public Map getMap() { return menuContextMap; } /** mainメソッド */ public static void main( String[] args ) throws Exception { MenuContextRetriever retriever = new MenuContextRetriever( "menu.xml" ); Map menuContextMap = retriever.getMap(); Iterator iterator = menuContextMap.keySet().iterator(); while ( iterator.hasNext() ) { String command = ( String ) iterator.next(); MenuContext menuContext = ( MenuContext ) menuContextMap.get( command ); String description = menuContext.getDescription(); } } } :::::::::::::: PanelRenderer.java :::::::::::::: import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.util.*; /** * $Id: gui-xml.html,v 1.1 2009/06/22 16:12:12 kishi Exp kishi $ * @author KISHI Yasuhiro */ public class PanelRenderer { public static void render( JPanel panel, MenuContext context ) { java.util.List list = context.getFormComponentList(); Iterator iterator = list.iterator(); while ( iterator.hasNext() ) { Map map = ( Map ) iterator.next(); String type = ( String ) map.get( "type" ); System.out.println( "type=" + type ); if ( "label".equals( type ) ) { String value = ( String ) map.get( "value" ); JLabel label = new JLabel( value ); panel.add( label ); } if ( "textfield".equals( type ) ) { String size = ( String ) map.get( "size" ); JTextField textField = new JTextField( new Integer( size ).intValue() ); panel.add( textField ); } if ( "submit".equals( type ) ) { String value = ( String ) map.get( "value" ); JButton button = new JButton( value ); panel.add( button ); } } } } :::::::::::::: menu.xml :::::::::::::: <?xml version="1.0" encoding="Shift_JIS" ?> <menu> <context> <command>CREATE_ITEM</command> <description>アイテム登録</description> <background>FF33FF</background> <form> <component type="label" value="アイテムID"/> <component type="textfield" size="20" name="ID"/> <component type="submit" value="送信"/> </form> </context> <context> <command>SYMBOL_DATA</command> <description>刻印データ出力</description> <background>FFFFFF</background> <form> <component type="submit" value="刻印対象のデータを出力する"/> </form> </context> <context> <command>INSPECTION</command> <description>検査</description> <background>6699CC</background> <form> <component type="label" value="アイテムID"/> <component type="textfield" size="20" name="ID"/> <component type="submit" value="送信"/> </form> </context> <context> <command>INPUT_TO_WAREHOUSE</command> <description>受入</description> <background>CC99CC</background> <form> <component type="label" value="アイテムID"/> <component type="textfield" size="20" name="ID"/> <component type="submit" value="送信"/> </form> </context> </menu>
戻る inserted by FC2 system