20 package net.sf.gridarta.gui.dialog.browsearchetypes;
22 import java.awt.BorderLayout;
23 import java.awt.Component;
24 import java.awt.Dimension;
25 import java.awt.event.ItemEvent;
26 import java.awt.event.ItemListener;
27 import java.awt.event.MouseEvent;
28 import java.awt.event.MouseListener;
30 import java.io.IOException;
31 import java.util.Collection;
32 import java.util.TreeSet;
33 import javax.swing.BoxLayout;
34 import javax.swing.DefaultComboBoxModel;
35 import javax.swing.ImageIcon;
36 import javax.swing.JButton;
37 import javax.swing.JComboBox;
38 import javax.swing.JDialog;
39 import javax.swing.JFileChooser;
40 import javax.swing.JOptionPane;
41 import javax.swing.JPanel;
42 import javax.swing.JScrollPane;
43 import javax.swing.JTable;
44 import javax.swing.ListSelectionModel;
45 import javax.swing.ScrollPaneConstants;
46 import javax.swing.SwingUtilities;
47 import javax.swing.WindowConstants;
48 import javax.swing.event.ListSelectionEvent;
49 import javax.swing.event.ListSelectionListener;
50 import javax.swing.table.JTableHeader;
51 import javax.swing.table.TableColumnModel;
62 import net.
sf.japi.swing.action.ActionBuilder;
63 import net.
sf.japi.swing.action.ActionBuilderFactory;
64 import net.
sf.japi.swing.action.ActionMethod;
65 import org.jetbrains.annotations.NotNull;
82 private static final ActionBuilder
ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder(
"net.sf.gridarta");
146 private final JFileChooser
chooser =
new JFileChooser();
159 public BrowseArchetypesDialog(@NotNull
final Component parentComponent, @NotNull
final ArchetypeChooserControl<G, A, R> archetypeChooserControl, @NotNull
final ObjectChooser<G, A, R> objectChooser, @NotNull
final ArchetypeSet<G, A, R> archetypeSet, @NotNull
final ImageIcon closingIcon) {
163 archetypesTable =
new JTable(archetypesTableModel);
166 availableAttributes.addAll(archetype.getAttributeKeys());
168 if (availableAttributes.remove(
"value")) {
172 archetypesTable.setFillsViewportHeight(
true);
173 archetypesTable.setAutoCreateRowSorter(
true);
174 archetypesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
175 archetypesTable.setRowSelectionAllowed(
true);
176 archetypesTable.setColumnSelectionAllowed(
false);
177 final ListSelectionListener listSelectionListener =
new ListSelectionListener() {
180 public void valueChanged(@NotNull
final ListSelectionEvent e) {
185 archetypesTable.getSelectionModel().addListSelectionListener(listSelectionListener);
186 final JTableHeader tableHeader = archetypesTable.getTableHeader();
187 tableHeader.setReorderingAllowed(
true);
189 tableHeader.setDefaultRenderer(renderer);
190 final MouseListener mouseListener =
new MouseListener() {
195 private boolean pressed;
198 public void mouseClicked(@NotNull
final MouseEvent e) {
201 if (SwingUtilities.isLeftMouseButton(e)) {
202 final TableColumnModel columnModel = archetypesTable.getColumnModel();
203 final int viewColumn = columnModel.getColumnIndexAtX(e.getX());
204 final int columnIndex = archetypesTable.convertColumnIndexToModel(viewColumn);
205 final int offsetX = renderer.
getX(columnIndex);
207 for (
int i = 0; i < columnIndex; i++) {
208 cx += columnModel.getColumn(i).getWidth();
210 final int ex = e.getX();
211 if (offsetX >= 0 && ex - cx >= offsetX) {
212 final String attribute = archetypesTableModel.
removeAttribute(columnIndex);
213 if (attribute != null) {
214 availableAttributes.add(attribute);
216 addAttributeComboBoxModel.setSelectedItem(attribute);
224 public void mousePressed(@NotNull
final MouseEvent e) {
229 public void mouseReleased(@NotNull
final MouseEvent e) {
233 public void mouseEntered(@NotNull
final MouseEvent e) {
237 public void mouseExited(@NotNull
final MouseEvent e) {
241 tableHeader.addMouseListener(mouseListener);
242 addAttributeButton =
new JButton(ACTION_BUILDER.createAction(
false,
"browseArchetypesAddAttribute",
this));
244 final ItemListener itemListener =
new ItemListener() {
247 public void itemStateChanged(@NotNull
final ItemEvent e) {
252 addAttributeComboBox.addItemListener(itemListener);
255 final JPanel addAttributePanel =
new JPanel();
256 addAttributePanel.setLayout(
new BoxLayout(addAttributePanel, BoxLayout.LINE_AXIS));
257 addAttributePanel.add(addAttributeButton);
258 addAttributePanel.add(addAttributeComboBox);
259 addAttributePanel.add(
new JButton(ACTION_BUILDER.createAction(
false,
"browseArchetypesSave",
this)));
260 setOptions(
new Object[] { addAttributePanel });
263 addAttributeComboBox.setSelectedIndex(Math.min(0, addAttributeComboBoxModel.getSize() - 1));
266 dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
267 dialog.setModal(
false);
268 dialog.setResizable(
true);
270 dialog.setMinimumSize(
new Dimension(200, 200));
271 dialog.setSize(
new Dimension(800, 600));
272 dialog.setLocationRelativeTo(parentComponent);
273 setInitialValue(archetypesTable);
284 dialog.setVisible(
true);
294 final JPanel mainPanel =
new JPanel(
new BorderLayout());
298 final JScrollPane scrollPane =
new JScrollPane(archetypesTable);
299 scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
300 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
302 mainPanel.add(scrollPane, BorderLayout.CENTER);
319 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
320 chooser.setMultiSelectionEnabled(
false);
322 final int returnVal = chooser.showSaveDialog(
this);
323 if (returnVal == JFileChooser.APPROVE_OPTION) {
324 File file = chooser.getSelectedFile();
327 file =
new File(file.getParentFile(), file.getName() +
".csv");
330 }
catch (
final IOException ex) {
331 ACTION_BUILDER.showMessageDialog(
this,
"browseArchetypesSaveIOException", file, ex.getMessage());
342 final String attributeName = (String) addAttributeComboBox.getSelectedItem();
343 if (attributeName == null) {
348 if (availableAttributes.remove(attributeName)) {
350 final int selectedIndex = Math.max(addAttributeComboBox.getSelectedIndex(), 0);
352 addAttributeComboBox.setSelectedIndex(Math.min(selectedIndex, addAttributeComboBoxModel.getSize() - 1));
355 }
else if (!availableAttributes.contains(attributeName)) {
367 addAttributeComboBox.setEnabled(!availableAttributes.isEmpty());
375 addAttributeComboBoxModel.removeAllElements();
376 for (
final String attribute : availableAttributes) {
377 addAttributeComboBoxModel.addElement(attribute);
386 final int index = archetypesTable.getSelectedRow();
391 final R archetype = archetypesTableModel.
get(archetypesTable.getRowSorter().convertRowIndexToModel(index));
void browseArchetypesSave()
Action method for saving the current list to a CSV file.
Graphical User Interface of Gridarta.
Reading and writing of Atrinik maps.
void addAttribute(@NotNull final String attribute)
Adds a column for an attribute to the table.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
JPanel createPanel()
Creates the GUI.
final JDialog dialog
The dialog.
static final FileFilter CSV_FILE_FILTER
Swing FileFilter for CSV files.
final ObjectChooser< G, A, R > objectChooser
The insertion object chooser to use when selecting search results.
Utility class for JFileChooser related functions.
final JButton addAttributeButton
The button for adding the selected attribute to archetypesTable.
boolean doAddAttribute(final boolean performAction)
Adds an attribute to the table.
A dialog that displays archetypes.
void updateComboBox()
Updates the contents of addAttributeComboBoxModel.
void moveArchetypeChooserToFront()
Move the Archetype Chooser in front of the Pickmap Chooser.
R get(final int index)
Return one archetype.
final JComboBox< String > addAttributeComboBox
The attribute to add to archetypesTable if addAttributeButton is selected.
void showDialog()
Opens the dialog.
void saveAsCsv(@NotNull final File file)
Saves the current contents as a CSV file.
static String getString(@NotNull final ActionBuilder actionBuilder, @NotNull final String key, @NotNull final String defaultValue)
Returns the value of a key.
final Collection< String > availableAttributes
The attribute names currently not shown.
Base package of all Gridarta classes.
Utility class defining FileFilters.
Reflects a game object (object on a map).
final JTable archetypesTable
The JTable showing the selected archetypes.
GameObjects are the objects based on Archetypes found on maps.
final DefaultComboBoxModel< String > addAttributeComboBoxModel
The model of addAttributeComboBox.
void highlightSelectedEntry()
Highlights the selected row from archetypesTable in the insertion object chooser. ...
void updateActions()
Updates the actions' states to reflect the current selection.
BrowseArchetypesDialog(@NotNull final Component parentComponent, @NotNull final ArchetypeChooserControl< G, A, R > archetypeChooserControl, @NotNull final ObjectChooser< G, A, R > objectChooser, @NotNull final ArchetypeSet< G, A, R > archetypeSet, @NotNull final ImageIcon closingIcon)
Creates a new instance.
void selectArchetype(@NotNull final R archetype)
Select an archetype.
void browseArchetypesAddAttribute()
Action method for "add attribute".
Utility class for ActionBuilder related functions.
Border DIALOG_BORDER
The Border object to be used when creating dialogs.
static void sanitizeCurrentDirectory(@NotNull final JFileChooser fileChooser)
Makes sure the current directory of a JFileChooser is valid.
Interface that captures similarities between different ArchetypeSet implementations.
Common base interface for ObjectChoosers.
final BrowseArchetypesTableModel< G, A, R > archetypesTableModel
The model of archetypesTable.
final ArchetypeChooserControl< G, A, R > archetypeChooserControl
The archetype set to search.
That control of the archetype chooser.
String removeAttribute(final int index)
Removes an attribute from the table.
static final long serialVersionUID
The serial Version UID.
Interface for MapArchObjects.
Defines common UI constants used in different dialogs.
final JFileChooser chooser
The JFileChooser for saving CSV files.