1 4 package org.oddjob.designer.view; 5 6 import java.awt.Component ; 7 import java.awt.Dialog ; 8 import java.awt.Frame ; 9 import java.awt.HeadlessException ; 10 import java.awt.Window ; 11 import java.awt.event.ActionEvent ; 12 13 import javax.swing.AbstractAction ; 14 import javax.swing.Action ; 15 import javax.swing.JButton ; 16 import javax.swing.JPopupMenu ; 17 import javax.swing.SwingUtilities ; 18 19 import org.oddjob.designer.Looks; 20 import org.oddjob.designer.model.ComponentAction; 21 import org.oddjob.designer.model.DesignDefinition; 22 23 26 public class ViewHelper { 27 28 35 public static Component createDetailButton(final DesignDefinition def) { 36 final JButton button = new JButton (); 37 button.setAction(new AbstractAction ("Edit") { 38 43 public void actionPerformed(ActionEvent e) { 44 Window w = SwingUtilities.windowForComponent(button); 45 Component form = ViewFactory.create(def).dialog(); 46 47 ValueDialog valueDialog = null; 48 if (w == null) { 49 valueDialog = new ValueDialog(form); 50 } 51 else { 52 if (w instanceof Frame ) { 53 valueDialog = new ValueDialog((Frame ) w, form); 54 } else { 55 valueDialog = new ValueDialog((Dialog ) w, form); 56 } 57 } 58 valueDialog.pack(); 59 valueDialog.setVisible(true); 60 } 61 }); 62 return button; 63 } 64 65 71 public static String padLabel(String title) { 72 StringBuffer paddedTitle = new StringBuffer (); 73 paddedTitle.append(title); 74 for (int i = title.length(); i < Looks.LABEL_SIZE; ++i) { 75 paddedTitle.append(' '); 76 } 77 return paddedTitle.toString(); 78 } 79 80 86 public static Action convert(final ComponentAction from) { 87 return new AbstractAction (from.getName()) { 88 public void actionPerformed(ActionEvent e) { 89 from.perform(); 90 } 91 }; 92 } 93 94 101 public static Action [] convertAll(ComponentAction[] froms) { 102 Action [] actions = new Action [froms.length]; 103 for (int i = 0; i < actions.length; ++i) { 104 actions[i] = convert(froms[i]); 105 } 106 return actions; 107 } 108 109 118 public static Window getWindowForComponent(Component parentComponent) 119 throws HeadlessException { 120 if (parentComponent == null) 121 return null; 122 if (parentComponent instanceof Frame || parentComponent instanceof Dialog ) 123 return (Window )parentComponent; 124 if (parentComponent instanceof JPopupMenu ) { 125 return ViewHelper.getWindowForComponent( 127 ((JPopupMenu ) parentComponent).getInvoker()); 128 } 129 return ViewHelper.getWindowForComponent(parentComponent.getParent()); 130 } 131 } | Popular Tags |