Skip to content

Commit

Permalink
Implement File, Directory, Java Class, Package component builders
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-wyluda committed Jul 9, 2014
1 parent 68c365e commit f683ffd
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2014 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.plugin.idea.ui.component;

import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import org.jboss.forge.addon.convert.Converter;
import org.jboss.forge.addon.convert.ConverterFactory;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.util.InputComponents;
import org.jboss.forge.plugin.idea.service.ServiceHelper;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;

/**
* @author Adam Wyłuda
*/
public abstract class AbstractFileChooserComponentBuilder extends ComponentBuilder
{
@Override
public ForgeComponent build(final InputComponent<?, Object> input)
{
return new LabeledComponent(input, new ForgeComponent()
{
@Override
public void buildUI(Container container)
{

final TextFieldWithBrowseButton fileField = createTextField();

// Set Default Value
final ConverterFactory converterFactory = ServiceHelper.getForgeService()
.getConverterFactory();
Converter<Object, String> converter = converterFactory.getConverter(
input.getValueType(), String.class);
String value = converter.convert(InputComponents.getValueFor(input));
fileField.setText(value == null ? "" : value);

final JTextField textField = fileField.getTextField();
textField.getDocument().addDocumentListener(new DocumentListener()
{
@Override
public void removeUpdate(DocumentEvent e)
{
InputComponents.setValueFor(converterFactory, input,
textField.getText());
valueChangeListener.run();
}

@Override
public void insertUpdate(DocumentEvent e)
{
InputComponents.setValueFor(converterFactory, input,
textField.getText());
valueChangeListener.run();
}

@Override
public void changedUpdate(DocumentEvent e)
{
InputComponents.setValueFor(converterFactory, input,
textField.getText());
valueChangeListener.run();
}
});

container.add(fileField);
}
});
}

@Override
protected Class<?>[] getSupportedInputComponentTypes()
{
return new Class<?>[]{UIInput.class};
}

protected abstract TextFieldWithBrowseButton createTextField();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2014 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.plugin.idea.ui.component;

import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.event.DocumentListener;
import com.intellij.ui.EditorTextFieldWithBrowseButton;
import org.jboss.forge.addon.convert.Converter;
import org.jboss.forge.addon.convert.ConverterFactory;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.util.InputComponents;
import org.jboss.forge.plugin.idea.service.ServiceHelper;

import java.awt.*;

/**
* @author Adam Wyłuda
*/
public abstract class AbstractJavaChooserComponentBuilder extends ComponentBuilder
{
@Override
public ForgeComponent build(final InputComponent<?, Object> input)
{
return new LabeledComponent(input, new ForgeComponent()
{
@Override
public void buildUI(Container container)
{
final EditorTextFieldWithBrowseButton fileField = createEditorField();

// Set Default Value
final ConverterFactory converterFactory = ServiceHelper.getForgeService()
.getConverterFactory();
Converter<Object, String> converter = converterFactory.getConverter(
input.getValueType(), String.class);
String value = converter.convert(InputComponents.getValueFor(input));
fileField.setText(value == null ? "" : value);
fileField.setButtonEnabled(true);

fileField.getChildComponent().addDocumentListener(new DocumentListener()
{
@Override
public void beforeDocumentChange(DocumentEvent event)
{
}

@Override
public void documentChanged(DocumentEvent event)
{
InputComponents.setValueFor(converterFactory, input,
fileField.getText());
valueChangeListener.run();
}
});

container.add(fileField);
}
});
}

@Override
protected Class<?>[] getSupportedInputComponentTypes()
{
return new Class<?>[]{UIInput.class};
}

protected abstract EditorTextFieldWithBrowseButton createEditorField();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
*/
package org.jboss.forge.plugin.idea.ui.component;

import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.util.InputComponents;
import org.jboss.forge.furnace.proxy.Proxies;

public abstract class ComponentBuilder
{
protected UIContext context;

public void setContext(UIContext context)
{
this.context = context;
}

/**
* Builds a UI Component object based on the input
* <p/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public enum ComponentBuilderRegistry
new ComboComponentBuilder(),
new RadioComponentBuilder(),
new FileChooserComponentBuilder(),
new DirectoryChooserComponentBuilder(),
new JavaClassChooserComponentBuilder(),
new JavaPackageChooserComponentBuilder(),
new TextBoxComponentBuilder(),
new PasswordComponentBuilder(),
new TextAreaComponentBuilder(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2014 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.plugin.idea.ui.component;

import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.plugin.idea.util.IDEUtil;

import java.io.File;

/**
* @author Adam Wyłuda
*/
public class DirectoryChooserComponentBuilder extends AbstractFileChooserComponentBuilder
{
@Override
protected TextFieldWithBrowseButton createTextField()
{
TextFieldWithBrowseButton textField = new TextFieldWithBrowseButton();
textField.addBrowseFolderListener("Select a directory", null, IDEUtil.projectFromContext(context),
FileChooserDescriptorFactory.createSingleLocalFileDescriptor());
return textField;
}

@Override
protected Class<?> getProducedType()
{
return File.class;
}

@Override
protected String getSupportedInputType()
{
return InputType.DIRECTORY_PICKER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,92 +8,20 @@

import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.ui.components.JBLabel;
import org.jboss.forge.addon.convert.Converter;
import org.jboss.forge.addon.convert.ConverterFactory;
import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.addon.ui.input.InputComponent;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.util.InputComponents;
import org.jboss.forge.plugin.idea.service.ServiceHelper;
import org.jboss.forge.plugin.idea.util.IDEUtil;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class FileChooserComponentBuilder extends ComponentBuilder
public class FileChooserComponentBuilder extends AbstractFileChooserComponentBuilder
{

@Override
public ForgeComponent build(final InputComponent<?, Object> input)
protected TextFieldWithBrowseButton createTextField()
{
return new ForgeComponent()
{
@Override
public void buildUI(Container container)
{
// Added Label
JBLabel label = new JBLabel(input.getLabel() == null ? input.getName()
: input.getLabel());
container.add(label);

final TextFieldWithBrowseButton fileField = new TextFieldWithBrowseButton(
new ActionListener()
{

@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub

}
}
);
// Set Default Value
final ConverterFactory converterFactory = ServiceHelper.getForgeService()
.getConverterFactory();
Converter<Object, String> converter = converterFactory.getConverter(
input.getValueType(), String.class);
String value = converter.convert(InputComponents.getValueFor(input));
fileField.setText(value == null ? "" : value);

final JTextField textField = fileField.getTextField();
textField.getDocument().addDocumentListener(new DocumentListener()
{
@Override
public void removeUpdate(DocumentEvent e)
{
InputComponents.setValueFor(converterFactory, input,
textField.getText());
valueChangeListener.run();
}

@Override
public void insertUpdate(DocumentEvent e)
{
InputComponents.setValueFor(converterFactory, input,
textField.getText());
valueChangeListener.run();
}

@Override
public void changedUpdate(DocumentEvent e)
{
InputComponents.setValueFor(converterFactory, input,
textField.getText());
valueChangeListener.run();
}
});

fileField.addBrowseFolderListener("Select a directory", null, null,
FileChooserDescriptorFactory.createSingleFolderDescriptor());
container.add(fileField);
}
};
TextFieldWithBrowseButton textField = new TextFieldWithBrowseButton();
textField.addBrowseFolderListener("Select a file", null, IDEUtil.projectFromContext(context),
FileChooserDescriptorFactory.createSingleLocalFileDescriptor());
return textField;
}

@Override
Expand All @@ -107,11 +35,4 @@ protected String getSupportedInputType()
{
return InputType.FILE_PICKER;
}

@Override
protected Class<?>[] getSupportedInputComponentTypes()
{
return new Class<?>[]{UIInput.class};
}

}

0 comments on commit f683ffd

Please sign in to comment.