public void testLayout() throws Exception { SwingMetawidget metawidget = new SwingMetawidget(); JComponent container = new JPanel(); // startLayout BoxLayout boxLayout = new BoxLayout(); boxLayout.startContainerLayout(container, metawidget); assertTrue(container.getLayout() instanceof javax.swing.BoxLayout); assertFalse((metawidget.getLayout() instanceof javax.swing.BoxLayout)); // layoutWidget assertEquals(0, container.getComponentCount()); Stub stub = new Stub(); boxLayout.layoutWidget(stub, PROPERTY, null, container, metawidget); assertEquals(0, container.getComponentCount()); stub.add(new JSpinner()); boxLayout.layoutWidget(stub, PROPERTY, null, container, metawidget); assertEquals(stub, container.getComponent(0)); assertEquals(1, container.getComponentCount()); boxLayout.layoutWidget(new JTextField(), PROPERTY, null, container, metawidget); assertTrue(container.getComponent(1) instanceof JTextField); assertEquals(2, container.getComponentCount()); }
public static void main(String[] args) { SwingMetawidget metawidget = new SwingMetawidget(); metawidget.setMetawidgetLayout( new SeparatorLayoutDecorator( new SeparatorLayoutDecoratorConfig() .setLayout( new SeparatorLayoutDecorator( new SeparatorLayoutDecoratorConfig() .setLayout(new org.metawidget.swing.layout.GridBagLayout()))))); metawidget.setToInspect(new Bar()); JFrame frame = new JFrame("Metawidget Tutorial"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(metawidget); frame.setSize(400, 210); frame.setVisible(true); }
public JComponent buildWidget( String elementName, Map<String, String> attributes, SwingMetawidget metawidget) { String[] exclude = (String[]) metawidget.getClientProperty("exclude"); if (ArrayUtils.contains(exclude, attributes.get(NAME))) { return new Stub(); } return null; }
@SuppressWarnings("unchecked") public void testWidgetBuilderExample() throws Exception { Person person = new Person(); SwingMetawidget metawidget = new SwingMetawidget(); metawidget.setWidgetBuilder( new CompositeWidgetBuilder<JComponent, SwingMetawidget>( new CompositeWidgetBuilderConfig<JComponent, SwingMetawidget>() .setWidgetBuilders(new ExcludingWidgetBuilder(), new SwingWidgetBuilder()))); metawidget.putClientProperty("exclude", new String[] {"age", "retired"}); metawidget.setToInspect(person); assertTrue(metawidget.getComponent(0) instanceof JLabel); assertTrue(metawidget.getComponent(1) instanceof JTextField); assertTrue(metawidget.getComponent(2) instanceof JPanel); assertTrue(3 == metawidget.getComponentCount()); }
@Override protected void addSectionWidget( String section, int level, JComponent container, SwingMetawidget metawidget) { JPanel separatorPanel = new JPanel(); separatorPanel.setBorder(BORDER_SECTION); separatorPanel.setLayout(new java.awt.GridBagLayout()); separatorPanel.setOpaque(false); // Section name (possibly localized) String localizedSection = metawidget.getLocalizedKey(StringUtils.camelCase(section)); if (localizedSection == null) { localizedSection = section; } GridBagConstraints labelConstraints = new GridBagConstraints(); GridBagConstraints separatorConstraints = new GridBagConstraints(); separatorConstraints.fill = GridBagConstraints.HORIZONTAL; separatorConstraints.weightx = 1.0; if (mAlignment == SwingConstants.RIGHT) { separatorConstraints.gridx = 0; labelConstraints.gridx = 1; labelConstraints.insets = INSETS_SECTION_LABEL_RIGHT; } else { labelConstraints.insets = INSETS_SECTION_LABEL_LEFT; } separatorPanel.add(new JLabel(localizedSection), labelConstraints); separatorPanel.add(new JSeparator(SwingConstants.HORIZONTAL), separatorConstraints); // Add to parent container Map<String, String> separatorPanelAttributes = CollectionUtils.newHashMap(); separatorPanelAttributes.put(LABEL, ""); separatorPanelAttributes.put(WIDE, TRUE); getDelegate() .layoutWidget(separatorPanel, PROPERTY, separatorPanelAttributes, container, metawidget); }
public void testEmptyStub() { SwingMetawidget metawidget = new SwingMetawidget(); metawidget.setMetawidgetLayout( new SeparatorLayoutDecorator( new SeparatorLayoutDecoratorConfig() .setLayout(new org.metawidget.swing.layout.GridBagLayout()))); metawidget.setToInspect(new Baz()); assertTrue(metawidget.getLayout() instanceof GridBagLayout); assertEquals(0, metawidget.getComponentCount()); metawidget.setMetawidgetLayout( new SeparatorLayoutDecorator( new SeparatorLayoutDecoratorConfig() .setLayout( new TabbedPaneLayoutDecorator( new TabbedPaneLayoutDecoratorConfig() .setLayout(new org.metawidget.swing.layout.GridBagLayout()))))); metawidget.setToInspect(new Baz()); assertEquals(0, metawidget.getComponentCount()); }
public JComponent buildWidget( String elementName, Map<String, String> attributes, SwingMetawidget metawidget) { // Not read-only? if (!WidgetBuilderUtils.isReadOnly(attributes)) { return null; } // Hidden if (TRUE.equals(attributes.get(HIDDEN))) { return new Stub(); } // Action if (ACTION.equals(elementName)) { JButton button = new JButton(metawidget.getLabelString(attributes)); button.setEnabled(false); return button; } // Masked (return a JPanel, so that we DO still render a label) if (TRUE.equals(attributes.get(MASKED))) { return new JPanel(); } // Lookups String lookup = attributes.get(LOOKUP); if (lookup != null && !"".equals(lookup)) { // May have alternate labels String lookupLabels = attributes.get(LOOKUP_LABELS); if (lookupLabels != null && !"".equals(lookupLabels)) { return new LookupLabel( CollectionUtils.newHashMap( CollectionUtils.fromString(lookup), CollectionUtils.fromString(lookupLabels))); } return new JLabel(); } // Lookup the Class Class<?> clazz = WidgetBuilderUtils.getActualClassOrType(attributes, String.class); if (clazz != null) { // Primitives if (clazz.isPrimitive()) { return new JLabel(); } if (String.class.equals(clazz)) { if (TRUE.equals(attributes.get(LARGE))) { // Do not use a JLabel: JLabels do not support carriage returns like JTextAreas // do, so a multi-line JTextArea formats to a single line JLabel. Instead use // a non-editable JTextArea within a borderless JScrollPane JTextArea textarea = new JTextArea(); // Since we know we are dealing with Strings, we consider // word-wrapping a sensible default textarea.setLineWrap(true); textarea.setWrapStyleWord(true); textarea.setEditable(false); // We also consider 2 rows a sensible default, so that the // read-only JTextArea is always distinguishable from a JLabel textarea.setRows(2); JScrollPane scrollPane = new JScrollPane(textarea); scrollPane.setBorder(null); return scrollPane; } return new JLabel(); } if (Character.class.equals(clazz)) { return new JLabel(); } if (Date.class.equals(clazz)) { return new JLabel(); } if (Boolean.class.equals(clazz)) { return new JLabel(); } if (Number.class.isAssignableFrom(clazz)) { return new JLabel(); } // Collections if (Collection.class.isAssignableFrom(clazz)) { return new Stub(); } } // Not simple, but don't expand if (TRUE.equals(attributes.get(DONT_EXPAND))) { return new JLabel(); } // Nested Metawidget return null; }
public void testNestedSeparators() { SwingMetawidget metawidget = new SwingMetawidget(); metawidget.setMetawidgetLayout( new SeparatorLayoutDecorator( new SeparatorLayoutDecoratorConfig() .setLayout( new SeparatorLayoutDecorator( new SeparatorLayoutDecoratorConfig() .setLayout(new org.metawidget.swing.layout.GridBagLayout()))))); metawidget.setToInspect(new Bar()); assertEquals("Abc:", ((JLabel) metawidget.getComponent(0)).getText()); assertTrue(metawidget.getComponent(1) instanceof JTextField); JPanel outerSeparator = (JPanel) metawidget.getComponent(2); assertEquals("Foo", ((JLabel) outerSeparator.getComponent(0)).getText()); JPanel innerSeparator = (JPanel) metawidget.getComponent(3); assertEquals("Bar", ((JLabel) innerSeparator.getComponent(0)).getText()); assertEquals("Def:", ((JLabel) metawidget.getComponent(4)).getText()); assertTrue(metawidget.getComponent(5) instanceof JCheckBox); assertEquals("Ghi:", ((JLabel) metawidget.getComponent(6)).getText()); assertTrue(metawidget.getComponent(7) instanceof JScrollPane); innerSeparator = (JPanel) metawidget.getComponent(8); assertEquals("Baz", ((JLabel) innerSeparator.getComponent(0)).getText()); assertEquals("Jkl:", ((JLabel) metawidget.getComponent(9)).getText()); assertTrue(metawidget.getComponent(10) instanceof JTextField); assertEquals("Mno:", ((JLabel) metawidget.getComponent(11)).getText()); assertTrue(metawidget.getComponent(12) instanceof JCheckBox); innerSeparator = (JPanel) metawidget.getComponent(13); assertEquals("Moo", ((JLabel) innerSeparator.getComponent(0)).getText()); assertEquals("Pqr:", ((JLabel) metawidget.getComponent(14)).getText()); assertTrue(metawidget.getComponent(15) instanceof JTextField); innerSeparator = (JPanel) metawidget.getComponent(16); assertEquals("Zoo", ((JLabel) innerSeparator.getComponent(0)).getText()); assertEquals("Zoo:", ((JLabel) metawidget.getComponent(17)).getText()); SwingMetawidget nestedMetawidget = (SwingMetawidget) metawidget.getComponent(18); assertEquals("Name:", ((JLabel) nestedMetawidget.getComponent(0)).getText()); assertEquals("Stu:", ((JLabel) metawidget.getComponent(19)).getText()); assertTrue(metawidget.getComponent(20) instanceof JTextField); assertEquals(21, metawidget.getComponentCount()); }
public void testAlignment() { SwingMetawidget metawidget = new SwingMetawidget(); metawidget.setToInspect(new Foo()); JPanel panel = (JPanel) metawidget.getComponent(0); assertEquals("Section", ((JLabel) panel.getComponent(0)).getText()); assertEquals( 0, ((GridBagLayout) panel.getLayout()).getConstraints(panel.getComponent(0)).insets.left); assertEquals( 5, ((GridBagLayout) panel.getLayout()).getConstraints(panel.getComponent(0)).insets.right); assertTrue(panel.getComponent(1) instanceof JSeparator); assertEquals("Bar:", ((JLabel) metawidget.getComponent(1)).getText()); assertTrue(metawidget.getComponent(2) instanceof JTextField); assertTrue(metawidget.getComponent(3) instanceof JPanel); assertEquals(4, metawidget.getComponentCount()); metawidget.setMetawidgetLayout( new SeparatorLayoutDecorator( new SeparatorLayoutDecoratorConfig() .setAlignment(SwingConstants.RIGHT) .setLayout(new org.metawidget.swing.layout.GridBagLayout()))); panel = (JPanel) metawidget.getComponent(0); assertEquals("Section", ((JLabel) panel.getComponent(0)).getText()); assertEquals( 1, ((GridBagLayout) panel.getLayout()).getConstraints(panel.getComponent(0)).gridx); assertEquals( 5, ((GridBagLayout) panel.getLayout()).getConstraints(panel.getComponent(0)).insets.left); assertEquals( 0, ((GridBagLayout) panel.getLayout()).getConstraints(panel.getComponent(0)).insets.right); assertTrue(panel.getComponent(1) instanceof JSeparator); assertEquals( 0, ((GridBagLayout) panel.getLayout()).getConstraints(panel.getComponent(1)).gridx); assertEquals("Bar:", ((JLabel) metawidget.getComponent(1)).getText()); assertTrue(metawidget.getComponent(2) instanceof JTextField); assertTrue(metawidget.getComponent(3) instanceof JPanel); assertEquals(4, metawidget.getComponentCount()); }