Moved UI components to community/platform/util

This commit is contained in:
Oleg Shpynov
2009-10-26 15:25:51 +03:00
parent 47cc84088f
commit 387fc80395
7 changed files with 347 additions and 0 deletions
@@ -0,0 +1,91 @@
package com.intellij.ui;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.basic.BasicRadioButtonUI;
import java.awt.*;
import java.awt.event.*;
/**
* @author oleg
*/
public class CheckBoxList extends JList {
private static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
private static final int DEFAULT_CHECK_BOX_WIDTH = 20;
public CheckBoxList(final ListModel dataModel, final CheckBoxListListener checkBoxListListener) {
super(dataModel);
setCellRenderer(new CellRenderer());
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
setBorder(BorderFactory.createEtchedBorder());
addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == ' ') {
int index = CheckBoxList.this.getSelectedIndex();
if (index >= 0) {
JCheckBox checkbox = (JCheckBox)getModel().getElementAt(index);
setSelected(checkbox, index, checkBoxListListener);
}
}
}
});
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (isEnabled()) {
int index = locationToIndex(e.getPoint());
if (index != -1) {
JCheckBox checkbox = (JCheckBox)getModel().getElementAt(index);
int iconArea;
try {
iconArea = ((BasicRadioButtonUI)checkbox.getUI()).getDefaultIcon().getIconWidth();
}
catch (ClassCastException c) {
iconArea = DEFAULT_CHECK_BOX_WIDTH;
}
if (e.getX() < iconArea) {
setSelected(checkbox, index, checkBoxListListener);
}
}
}
}
});
}
private void setSelected(JCheckBox checkbox, int index, CheckBoxListListener checkBoxListListener) {
boolean value = !checkbox.isSelected();
checkbox.setSelected(value);
repaint();
checkBoxListListener.checkBoxSelectionChanged(index, value);
}
private class CellRenderer implements ListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JCheckBox checkbox = (JCheckBox)value;
checkbox.setBackground(getBackgound(isSelected, checkbox));
checkbox.setForeground(getForeGround(isSelected, checkbox));
checkbox.setEnabled(isEnabled());
checkbox.setFont(getFont(checkbox));
checkbox.setFocusPainted(false);
checkbox.setBorderPainted(true);
checkbox.setBorder(isSelected ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);
return checkbox;
}
}
protected Font getFont(final JCheckBox checkbox) {
return getFont();
}
protected Color getBackgound(final boolean isSelected, final JCheckBox checkbox) {
return isSelected ? getSelectionBackground() : getBackground();
}
protected Color getForeGround(final boolean isSelected, final JCheckBox checkbox) {
return isSelected ? getSelectionForeground() : getForeground();
}
}
@@ -0,0 +1,8 @@
package com.intellij.ui;
/**
* @author oleg
*/
public interface CheckBoxListListener {
void checkBoxSelectionChanged(int index, boolean value);
}
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.ui.OptionalChooserComponent">
<grid id="27dc6" binding="myContentPane" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="7" bottom="7" right="7"/>
<constraints>
<xy x="20" y="20" width="455" height="247"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<scrollpane id="4c4f5">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="1e69c" class="com.intellij.ui.CheckBoxList" binding="myList" custom-create="true">
<constraints/>
<properties>
<selectionMode value="0"/>
</properties>
</component>
</children>
</scrollpane>
</children>
</grid>
</form>
@@ -0,0 +1,69 @@
package com.intellij.ui;
import com.intellij.openapi.util.Pair;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author oleg
* This component represents a list of checkboxes.
*/
public abstract class OptionalChooserComponent implements CheckBoxListListener {
private JPanel myContentPane;
private CheckBoxList myList;
private DefaultListModel myListModel;
private List<Pair<String, Boolean>> myInitialList;
private ArrayList<Pair<String, Boolean>> myWorkingList;
public OptionalChooserComponent(@NotNull final List<Pair<String, Boolean>> list) {
myInitialList = list;
myWorkingList = new ArrayList<Pair<String, Boolean>>(myInitialList);
// fill list
reset();
}
public JPanel getContentPane() {
return myContentPane;
}
public void checkBoxSelectionChanged(int index, boolean value) {
final Pair<String, Boolean> pair = myWorkingList.remove(index);
myWorkingList.add(index, Pair.create(pair.first, value));
}
private void createUIComponents() {
myListModel = new DefaultListModel();
myList = new CheckBoxList(myListModel, this);
}
public void reset() {
myWorkingList = new ArrayList<Pair<String, Boolean>>(myInitialList);
update();
}
public abstract JCheckBox createCheckBox(final String path, final boolean checked);
public boolean isModified() {
return !myWorkingList.equals(myInitialList);
}
public ArrayList<Pair<String, Boolean>> getValue() {
return myWorkingList;
}
public void apply() {
myInitialList.clear();
myInitialList.addAll(myWorkingList);
}
public void update() {
myListModel.clear();
for (Pair<String, Boolean> pair : myWorkingList) {
myListModel.addElement(createCheckBox(pair.first, pair.second));
}
}
}
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.ui.PathsChooserComponent">
<grid id="27dc6" binding="myContentPane" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="7" bottom="7" right="7"/>
<constraints>
<xy x="20" y="20" width="591" height="247"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<vspacer id="e6d11">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<scrollpane id="837a">
<constraints>
<grid row="0" column="0" row-span="3" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="8b3da" class="javax.swing.JList" binding="myList">
<constraints/>
<properties>
<selectionMode value="0"/>
</properties>
</component>
</children>
</scrollpane>
<component id="360f6" class="javax.swing.JButton" binding="myAddButton" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/UIBundle" key="row.add"/>
</properties>
</component>
<component id="f80a3" class="javax.swing.JButton" binding="myRemoveButton" default-binding="true">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/UIBundle" key="row.remove"/>
</properties>
</component>
</children>
</grid>
</form>
@@ -0,0 +1,101 @@
package com.intellij.ui;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.fileChooser.FileChooserDialog;
import com.intellij.openapi.fileChooser.FileChooserFactory;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
/**
* @author oleg
* This component is used to configure list of folders with add/remove buttons.
*/
public class PathsChooserComponent {
private JPanel myContentPane;
private JList myList;
private JButton myAddButton;
private JButton myRemoveButton;
private DefaultListModel myListModel;
private List<String> myWorkingCollection;
private List<String> myInitialCollection;
public PathsChooserComponent(@NotNull final List<String> collection, @NotNull final PathProcessor processor) {
myInitialCollection = collection;
myWorkingCollection = new ArrayList<String>(myInitialCollection);
myListModel = new DefaultListModel();
myList.setModel(myListModel);
// fill list
reset();
// listeners
myAddButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final FileChooserDescriptor dirChooser = FileChooserDescriptorFactory.createSingleFolderDescriptor();
dirChooser.setShowFileSystemRoots(true);
dirChooser.setHideIgnored(true);
dirChooser.setTitle(UIBundle.message("file.chooser.default.title"));
FileChooserDialog chooser = FileChooserFactory.getInstance().createFileChooser(dirChooser, myContentPane);
VirtualFile[] files = chooser.choose(null, null);
for (VirtualFile file : files) {
// adding to the end
final String path = file.getPath();
if (processor.addPath(myWorkingCollection, path)){
myListModel.addElement(path);
}
}
}
});
myRemoveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selected = myList.getSelectedIndex();
if (selected != -1) {
// removing index
final String path = (String) myListModel.get(selected);
if (processor.removePath(myWorkingCollection, path)){
myListModel.remove(selected);
}
}
}
});
}
public JPanel getContentPane() {
return myContentPane;
}
public List<String> getValues(){
return myWorkingCollection;
}
public void reset() {
myListModel.clear();
myWorkingCollection = new ArrayList<String>(myInitialCollection);
for (String path : myWorkingCollection) {
myListModel.addElement(path);
}
}
public boolean isModified() {
return !myWorkingCollection.equals(myInitialCollection);
}
public void apply() {
myInitialCollection.clear();
myInitialCollection.addAll(myWorkingCollection);
}
public interface PathProcessor {
boolean addPath(List<String> paths, String path);
boolean removePath(List<String> paths, String path);
}
}
+1
View File
@@ -16,6 +16,7 @@
<orderEntry type="module" module-name="platform-resources-en" />
<orderEntry type="library" name="OroMatcher" level="project" />
<orderEntry type="module" module-name="annotations" exported="" />
<orderEntry type="module" module-name="platform-api" />
</component>
<component name="copyright">
<Base>