Skip to content

Commit

Permalink
Refactor ComponentBuilder (introduced ForgeComponent class)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-wyluda committed Jun 8, 2014
1 parent ddd9756 commit 04fdfc1
Show file tree
Hide file tree
Showing 10 changed files with 403 additions and 342 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,41 @@ public class CheckboxComponentBuilder extends ComponentBuilder
{

@Override
public JComponent build(final InputComponent<?, Object> input,
Container container)
public ForgeComponent build(final InputComponent<?, Object> input)
{
// Create the label
String text = (input.getLabel() == null ? input.getName() : input
.getLabel());
final JCheckBox checkbox = new JCheckBox(text);
// Set Default Value
final ConverterFactory converterFactory = ServiceHelper.getForgeService()
.lookup(ConverterFactory.class);
if (converterFactory != null)
{
Converter<Object, Boolean> converter = converterFactory
.getConverter(input.getValueType(), Boolean.class);
Boolean value = converter.convert(InputComponents
.getValueFor(input));
checkbox.setSelected(value == null ? false : value);
}
checkbox.addActionListener(new ActionListener()
return new ForgeComponent()
{
@Override
public void actionPerformed(ActionEvent e)
public void buildUI(Container container)
{
InputComponents.setValueFor(converterFactory, input,
checkbox.isSelected());
valueChangeListener.run();
// Create the label
String text = (input.getLabel() == null ? input.getName() : input
.getLabel());
final JCheckBox checkbox = new JCheckBox(text);
// Set Default Value
final ConverterFactory converterFactory = ServiceHelper.getForgeService()
.lookup(ConverterFactory.class);
if (converterFactory != null)
{
Converter<Object, Boolean> converter = converterFactory
.getConverter(input.getValueType(), Boolean.class);
Boolean value = converter.convert(InputComponents
.getValueFor(input));
checkbox.setSelected(value == null ? false : value);
}
checkbox.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
InputComponents.setValueFor(converterFactory, input,
checkbox.isSelected());
valueChangeListener.run();
}
});
container.add(checkbox, "span 2");
}
});
container.add(checkbox, "span 2");
return checkbox;
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,68 +27,74 @@ public class ComboComponentBuilder extends ComponentBuilder

@SuppressWarnings("unchecked")
@Override
public JComponent build(final InputComponent<?, Object> input, Container container)
public ForgeComponent build(final InputComponent<?, Object> input)
{
// Create the label
JBLabel label = new JBLabel();
label.setText(input.getLabel() == null ? input.getName() : input
.getLabel());
container.add(label);

final ConverterFactory converterFactory = ServiceHelper.getForgeService()
.getConverterFactory();
final UISelectOne<Object> selectOne = (UISelectOne<Object>) input;
final Converter<Object, String> converter = (Converter<Object, String>) InputComponents
.getItemLabelConverter(converterFactory, selectOne);
final DefaultComboBoxModel model = new DefaultComboBoxModel();

ComboBox combo = new ComboBox(model);
combo.setRenderer(new ListCellRenderer()
return new ForgeComponent()
{

@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus)
public void buildUI(Container container)
{
Object obj = model.getElementAt(index);
return new JLabel(converter.convert(obj));
}
});
container.add(combo);
String value = converter.convert(InputComponents.getValueFor(input));
Iterable<Object> valueChoices = selectOne.getValueChoices();
if (valueChoices != null)
{
for (Object choice : valueChoices)
{
model.addElement(Proxies.unwrap(choice));
}
}
combo.addItemListener(new ItemListener()
{
// Create the label
JBLabel label = new JBLabel();
label.setText(input.getLabel() == null ? input.getName() : input
.getLabel());
container.add(label);

@Override
public void itemStateChanged(ItemEvent e)
{
Object selectedItem = model.getSelectedItem();
InputComponents.setValueFor(converterFactory, input, selectedItem);
valueChangeListener.run();
}
});
final ConverterFactory converterFactory = ServiceHelper.getForgeService()
.getConverterFactory();
final UISelectOne<Object> selectOne = (UISelectOne<Object>) input;
final Converter<Object, String> converter = (Converter<Object, String>) InputComponents
.getItemLabelConverter(converterFactory, selectOne);
final DefaultComboBoxModel model = new DefaultComboBoxModel();

// Set Default Value
if (value == null)
{
if (model.getSize() > 0)
{
model.setSelectedItem(model.getElementAt(0));
ComboBox combo = new ComboBox(model);
combo.setRenderer(new ListCellRenderer()
{

@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus)
{
Object obj = model.getElementAt(index);

This comment has been minimized.

Copy link
@gastaldi

gastaldi Jun 8, 2014

Member

Isn't this the value parameter?

This comment has been minimized.

Copy link
@adam-wyluda

adam-wyluda Jun 8, 2014

Author Member

I didn't write this code, but I think you are right, this is the same thing :P. I will be working on component builders soon.

return new JLabel(converter.convert(obj));
}
});
container.add(combo);
String value = converter.convert(InputComponents.getValueFor(input));
Iterable<Object> valueChoices = selectOne.getValueChoices();
if (valueChoices != null)
{
for (Object choice : valueChoices)
{
model.addElement(Proxies.unwrap(choice));
}
}
combo.addItemListener(new ItemListener()
{

@Override
public void itemStateChanged(ItemEvent e)
{
Object selectedItem = model.getSelectedItem();
InputComponents.setValueFor(converterFactory, input, selectedItem);
valueChangeListener.run();
}
});

// Set Default Value
if (value == null)
{
if (model.getSize() > 0)
{
model.setSelectedItem(model.getElementAt(0));
}
}
else
{
model.setSelectedItem(value);
}
}
}
else
{
model.setSelectedItem(value);
}
return combo;
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,65 +28,71 @@ public class ComboEnumComponentBuilder extends ComponentBuilder
{
@SuppressWarnings("unchecked")
@Override
public JComponent build(final InputComponent<?, Object> input, Container container)
public ForgeComponent build(final InputComponent<?, Object> input)
{
// Create the label
JBLabel label = new JBLabel();
label.setText(input.getLabel() == null ? input.getName() : input
.getLabel());
container.add(label);

final ConverterFactory converterFactory = ServiceHelper.getForgeService()
.getConverterFactory();
final UISelectOne<Object> selectOne = (UISelectOne<Object>) input;
final Converter<Object, String> converter = (Converter<Object, String>) InputComponents
.getItemLabelConverter(converterFactory, selectOne);
final DefaultComboBoxModel model = new DefaultComboBoxModel();
Enum[] enumConstants = input.getValueType().asSubclass(Enum.class)
.getEnumConstants();
for (Enum enum1 : enumConstants)
{
model.addElement(enum1.name());
}

ComboBox combo = new ComboBox(model);
container.add(combo);
String value = converter.convert(InputComponents.getValueFor(input));
Iterable<Object> valueChoices = selectOne.getValueChoices();
if (valueChoices != null)
return new ForgeComponent()
{
for (Object choice : valueChoices)
{
model.addElement(Proxies.unwrap(choice));
}
}
combo.addItemListener(new ItemListener()
{

@Override
public void itemStateChanged(ItemEvent e)
public void buildUI(Container container)
{
String selectedItem = (String) model.getSelectedItem();
Class valueType = input.getValueType();
InputComponents.setValueFor(converterFactory, input,
Enum.valueOf(valueType, selectedItem));
valueChangeListener.run();
}
});
// Create the label
JBLabel label = new JBLabel();
label.setText(input.getLabel() == null ? input.getName() : input
.getLabel());
container.add(label);

// Set Default Value
if (value == null)
{
if (model.getSize() > 0)
{
model.setSelectedItem(model.getElementAt(0));
final ConverterFactory converterFactory = ServiceHelper.getForgeService()
.getConverterFactory();
final UISelectOne<Object> selectOne = (UISelectOne<Object>) input;
final Converter<Object, String> converter = (Converter<Object, String>) InputComponents
.getItemLabelConverter(converterFactory, selectOne);
final DefaultComboBoxModel model = new DefaultComboBoxModel();
Enum[] enumConstants = input.getValueType().asSubclass(Enum.class)
.getEnumConstants();
for (Enum enum1 : enumConstants)
{
model.addElement(enum1.name());
}

ComboBox combo = new ComboBox(model);
container.add(combo);
String value = converter.convert(InputComponents.getValueFor(input));
Iterable<Object> valueChoices = selectOne.getValueChoices();
if (valueChoices != null)
{
for (Object choice : valueChoices)
{
model.addElement(Proxies.unwrap(choice));
}
}
combo.addItemListener(new ItemListener()
{

@Override
public void itemStateChanged(ItemEvent e)
{
String selectedItem = (String) model.getSelectedItem();
Class valueType = input.getValueType();
InputComponents.setValueFor(converterFactory, input,
Enum.valueOf(valueType, selectedItem));
valueChangeListener.run();
}
});

// Set Default Value
if (value == null)
{
if (model.getSize() > 0)
{
model.setSelectedItem(model.getElementAt(0));
}
}
else
{
model.setSelectedItem(value);
}
}
}
else
{
model.setSelectedItem(value);
}
return combo;
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,20 @@
package org.jboss.forge.plugin.idea.ui.component;

import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.output.UIMessage;
import org.jboss.forge.addon.ui.util.InputComponents;
import org.jboss.forge.furnace.proxy.Proxies;

import javax.swing.*;
import java.awt.*;

public abstract class ComponentBuilder
{
protected Runnable valueChangeListener;

/**
* Builds a UI Component object based on the input
* <p/>
* TODO Write JavaDoc
*
* @param input
* @param container
* @return
*/
public abstract JComponent build(final InputComponent<?, Object> input,
final Container container);
public abstract ForgeComponent build(InputComponent<?, Object> input);

/**
* Returns the supported type this control may produce
Expand Down Expand Up @@ -88,33 +80,4 @@ public boolean handles(InputComponent<?, ?> input)

return handles;
}

public void setValueChangeListener(Runnable valueChangeListener)
{
this.valueChangeListener = valueChangeListener;
}

/**
* Displays validation messages.
*/
public void displayMessages(java.util.List<UIMessage> messages)
{
for (UIMessage message : messages)
{
if (message.getSeverity().equals(UIMessage.Severity.ERROR))
{
setErrorMessage(message);
return;
}
}
clearErrorMessage();
}

public void setErrorMessage(UIMessage message)
{
}

public void clearErrorMessage()
{
}
}
Loading

0 comments on commit 04fdfc1

Please sign in to comment.