/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.napile.idea.thermit.config.impl.configuration;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.text.BadLocationException;
import org.napile.idea.thermit.ThermitBundle;
import org.napile.idea.thermit.ThermitIcons;
import org.napile.idea.thermit.config.impl.AntClasspathEntry;
import org.napile.idea.thermit.config.impl.AntInstallation;
import org.napile.idea.thermit.config.impl.AntReference;
import org.napile.idea.thermit.config.impl.GlobalThermitConfiguration;
import com.intellij.icons.AllIcons;
import com.intellij.ide.macro.MacrosDialog;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ui.OrderEntryAppearanceService;
import com.intellij.openapi.ui.FixedSizeButton;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.ui.ColoredListCellRenderer;
import com.intellij.ui.SimpleColoredComponent;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.util.IconUtil;
import com.intellij.util.PlatformIcons;
import com.intellij.util.config.AbstractProperty;
import com.intellij.util.ui.AbstractTableCellEditor;
import com.intellij.util.ui.CellEditorComponentWithBrowseButton;
public class AntUIUtil
{
private static final Logger LOG = Logger.getInstance("#com.intellij.thermit.impl.configuration.AntUIUtil");
private AntUIUtil()
{
}
public interface PropertiesEditor<T>
{
AbstractProperty.AbstractPropertyContainer getProperties(T object);
}
public static class AntInstallationRenderer extends ColoredListCellRenderer
{
private final PropertiesEditor<AntInstallation> myEditor;
public AntInstallationRenderer(PropertiesEditor<AntInstallation> editor)
{
myEditor = editor != null ? editor : new PropertiesEditor<AntInstallation>()
{
public AbstractProperty.AbstractPropertyContainer getProperties(AntInstallation antInstallation)
{
return antInstallation.getProperties();
}
};
}
protected void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus)
{
AntInstallation ant = (AntInstallation) value;
if(ant == null)
return;
AbstractProperty.AbstractPropertyContainer container = myEditor.getProperties(ant);
customizeAnt(container, this);
}
}
public static class AntReferenceRenderer extends ColoredListCellRenderer
{
private final GlobalThermitConfiguration myConfiguration;
public AntReferenceRenderer(GlobalThermitConfiguration configuration)
{
myConfiguration = configuration;
}
protected void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus)
{
if(value == null)
return;
customizeReference((AntReference) value, this, myConfiguration);
}
}
public static void customizeReference(AntReference antReference, SimpleColoredComponent component, GlobalThermitConfiguration configuration)
{
AntInstallation antInstallation = antReference.find(configuration);
if(antInstallation != null)
customizeAnt(antInstallation.getProperties(), component);
else
{
component.setIcon(PlatformIcons.INVALID_ENTRY_ICON);
component.append(antReference.getName(), SimpleTextAttributes.ERROR_ATTRIBUTES);
}
}
public static void customizeAnt(AbstractProperty.AbstractPropertyContainer antProperties, SimpleColoredComponent component)
{
component.setIcon(ThermitIcons.TOOLBAR_ICON);
String name = AntInstallation.NAME.get(antProperties);
component.append(name, SimpleTextAttributes.REGULAR_ATTRIBUTES);
String versionString = AntInstallation.VERSION.get(antProperties);
if(name.indexOf(versionString) == -1)
component.append(" (" + versionString + ")", SimpleTextAttributes.SYNTHETIC_ATTRIBUTES);
}
public static class ClasspathRenderer extends ColoredListCellRenderer
{
protected void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus)
{
AntClasspathEntry entry = (AntClasspathEntry) value;
entry.getAppearance().customize(this);
}
}
public static class PropertyValueCellEditor extends AbstractTableCellEditor
{
private final CellEditorComponentWithBrowseButton<JTextField> myComponent;
public PropertyValueCellEditor()
{
myComponent = new CellEditorComponentWithBrowseButton<JTextField>(new TextFieldWithBrowseButton(), this);
getChildComponent().setBorder(BorderFactory.createLineBorder(Color.black));
FixedSizeButton button = myComponent.getComponentWithButton().getButton();
button.setIcon(IconUtil.getAddIcon());
button.setToolTipText(ThermitBundle.message("ant.property.value.editor.insert.macro.tooltip.text"));
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
MacrosDialog dialog = new MacrosDialog(getChildComponent());
dialog.show();
if(dialog.isOK() && dialog.getSelectedMacro() != null)
{
JTextField textField = getChildComponent();
String macro = dialog.getSelectedMacro().getName();
int position = textField.getCaretPosition();
try
{
textField.getDocument().insertString(position, "$" + macro + "$", null);
textField.setCaretPosition(position + macro.length() + 2);
}
catch(BadLocationException ex)
{
LOG.error(ex);
}
textField.requestFocus();
}
}
});
}
public Object getCellEditorValue()
{
return getChildComponent().getText();
}
protected void initializeEditor(Object value)
{
getChildComponent().setText((String) value);
}
private JTextField getChildComponent()
{
return myComponent.getChildComponent();
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
{
getChildComponent().setText((String) value);
return myComponent;
}
}
public static class ProjectJdkRenderer extends ColoredListCellRenderer
{
private final boolean myInComboBox;
private final String myProjectJdkName;
public ProjectJdkRenderer(boolean inComboBox, String projectJdkName)
{
myInComboBox = inComboBox;
myProjectJdkName = projectJdkName != null ? projectJdkName : "";
}
protected void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus)
{
String jdkName = (String) value;
if(jdkName == null || jdkName.length() == 0)
jdkName = "";
Sdk jdk = GlobalThermitConfiguration.findJdk(jdkName);
if(jdk == null)
{
if(myProjectJdkName.length() > 0)
{
setIcon(AllIcons.General.Jdk);
append(ThermitBundle.message("project.jdk.project.jdk.name.list.column.value", myProjectJdkName), selected ? SimpleTextAttributes.SELECTED_SIMPLE_CELL_ATTRIBUTES : SimpleTextAttributes.SIMPLE_CELL_ATTRIBUTES);
}
else
{
setIcon(PlatformIcons.INVALID_ENTRY_ICON);
append(ThermitBundle.message("project.jdk.not.specified.list.column.value"), SimpleTextAttributes.ERROR_ATTRIBUTES);
}
}
else
{
OrderEntryAppearanceService.getInstance().forJdk(jdk, myInComboBox, selected, true).customize(this);
}
}
}
}