/** Read the map directory and fill the map list with the required data. */ @SuppressWarnings("nls") private void populateMaplist() { final File mapDir = MapEditor.getConfig().getFile("mapDir"); mapList.removeAll(); if (!mapDir.isDirectory()) { return; } final ArrayList<String> validMapNames = new ArrayList<String>(); final String[] mapFiles = mapDir.list(mapFileSearchFilter); for (final String mapFile : mapFiles) { final String mapName = mapFile.substring(0, mapFile.length() - 10); if (validMapNames.contains(mapName)) { mapList.add(mapName); } else { validMapNames.add(mapName); } } }
/** Constructor for the dialog that prepares the display of the loading screen correctly. */ @SuppressWarnings("nls") public MapLoader() { super(MapEditor.getMainFrame(), "Load map", true); final Panel content = new Panel(new BorderLayout(5, 5)); content.add( new Label("Select the maps you want to load. " + "Selecting multiple entries is possible."), BorderLayout.NORTH); mapList = new List(10, true); content.add(mapList, BorderLayout.CENTER); final Panel buttonPanel = new Panel(new FlowLayout(FlowLayout.CENTER, 10, 0)); final Button okButton = new Button("Load maps"); final Button cancelButton = new Button("Cancel"); buttonPanel.add(okButton); buttonPanel.add(cancelButton); content.add(buttonPanel, BorderLayout.SOUTH); add(content); pack(); validate(); setLocation(100, 100); final Dimension prefSize = getPreferredSize(); prefSize.width = 300; setPreferredSize(prefSize); cancelButton.addActionListener( new ActionListener() { @Override public void actionPerformed(final ActionEvent e) { setVisible(false); } }); okButton.addActionListener( new ActionListener() { @Override public void actionPerformed(final ActionEvent e) { final String[] selectedMaps = getMapList().getSelectedItems(); setVisible(false); for (final String selectedMap : selectedMaps) { MapStorage.getInstance().loadMap(selectedMap); } } }); addWindowListener( new WindowAdapter() { @Override public void windowClosing(final WindowEvent e) { setVisible(false); } }); mapFileSearchFilter = new FilenameFilter() { private final String itemFile = ".items.txt"; private final String tileFile = ".tiles.txt"; @Override public boolean accept(final File dir, final String name) { return name.endsWith(itemFile) || name.endsWith(tileFile); } }; }
/** @author Tim */ public class MapDialogs { private static final int UNSIGNED_MAX = 100000; private static final int SIGNED_MAX = 10000; private static final FilenameFilter FILTER_TILES = new FilenameFilter() { @Override public boolean accept(final File dir, final String name) { return name.endsWith(MapIO.EXT_TILE); } }; private static File saveDir; private static final Config config = MapEditor.getConfig(); private MapDialogs() {} public static Map showNewMapDialog(final JFrame owner) { final JDialog dialog = new JDialog(owner, Lang.getMsg("gui.newmap")); dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.PAGE_AXIS)); dialog.setResizable(false); dialog.setLocationRelativeTo(null); dialog.setModal(true); final JSpinner width = new JSpinner(new SpinnerNumberModel(100, 0, UNSIGNED_MAX, 1)); final JSpinner height = new JSpinner(new SpinnerNumberModel(100, 0, UNSIGNED_MAX, 1)); final JSpinner x = new JSpinner(new SpinnerNumberModel(0, -SIGNED_MAX, SIGNED_MAX, 1)); final JSpinner y = new JSpinner(new SpinnerNumberModel(0, -SIGNED_MAX, SIGNED_MAX, 1)); final JSpinner l = new JSpinner(new SpinnerNumberModel(0, -SIGNED_MAX, SIGNED_MAX, 1)); final JTextField name = new JTextField(1); final JButton btn = new JButton(Lang.getMsg("gui.newmap.Ok")); saveDir = null; btn.addActionListener( new ActionListener() { @Override public void actionPerformed(final ActionEvent e) { final JFileChooser ch = new JFileChooser(MapEditor.getConfig().getFile("mapLastOpenDir")); ch.setDialogType(JFileChooser.OPEN_DIALOG); ch.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if (ch.showOpenDialog(MainFrame.getInstance()) != JFileChooser.APPROVE_OPTION) { dialog.setVisible(false); return; } saveDir = ch.getSelectedFile(); dialog.setVisible(false); } }); dialog.add(new JLabel(Lang.getMsg("gui.newmap.Width"))); dialog.add(width); dialog.add(new JLabel(Lang.getMsg("gui.newmap.Height"))); dialog.add(height); dialog.add(new JLabel(Lang.getMsg("gui.newmap.X"))); dialog.add(x); dialog.add(new JLabel(Lang.getMsg("gui.newmap.Y"))); dialog.add(y); dialog.add(new JLabel(Lang.getMsg("gui.newmap.Z"))); dialog.add(l); dialog.add(new JLabel(Lang.getMsg("gui.newmap.Name"))); dialog.add(name); dialog.add(btn); dialog.doLayout(); dialog.pack(); dialog.setVisible(true); dialog.dispose(); if (saveDir != null) { return new Map( name.getText(), saveDir.getPath(), (Integer) width.getValue(), (Integer) height.getValue(), (Integer) x.getValue(), (Integer) y.getValue(), (Integer) l.getValue()); } return null; } public static Map[] showOpenMapDialog(final JFrame owner) throws IOException { final JFileChooser ch = new JFileChooser(); if (config.getFile("mapLastOpenDir") != null) { ch.setCurrentDirectory(config.getFile("mapLastOpenDir")); } ch.setDialogType(JFileChooser.OPEN_DIALOG); ch.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if (ch.showOpenDialog(MainFrame.getInstance()) != JFileChooser.APPROVE_OPTION) { return null; } final File dir = ch.getSelectedFile(); config.set("mapLastOpenDir", dir); final String[] maps = dir.list(FILTER_TILES); for (int i = 0; i < maps.length; ++i) { maps[i] = maps[i].substring(0, maps[i].length() - MapIO.EXT_TILE.length()); } final JDialog dialog = new JDialog(owner, Lang.getMsg("gui.chooser")); dialog.setModal(true); dialog.setLocationRelativeTo(null); dialog.setLayout(new BorderLayout()); final JList list = new JList(maps); final JButton btn = new JButton(Lang.getMsg("gui.chooser.Ok")); btn.addActionListener( new AbstractAction() { @Override public void actionPerformed(final ActionEvent e) { if (list.getSelectedValue() != null) { dialog.setVisible(false); } } }); dialog.add(new JScrollPane(list), BorderLayout.CENTER); dialog.add(btn, BorderLayout.SOUTH); dialog.pack(); dialog.setVisible(true); dialog.dispose(); Map[] loadedMaps = new Map[list.getSelectedIndices().length]; for (int i = 0; i < list.getSelectedIndices().length; i++) { loadedMaps[i] = MapIO.loadMap(dir.getPath(), (String) list.getSelectedValues()[i]); } return loadedMaps; } public static boolean isShowSaveDialog() { final int answer = JOptionPane.showConfirmDialog( null, Lang.getMsg("gui.info.unsaved"), Lang.getMsg("gui.info.unsaved.Title"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); return answer == JOptionPane.YES_OPTION; } }