PageRenderTime 141ms CodeModel.GetById 40ms app.highlight 13ms RepoModel.GetById 57ms app.codeStats 0ms

/src/mpv5/ui/popups/FileTablePopUp.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 180 lines | 110 code | 28 blank | 42 comment | 5 complexity | 0ee8c6d4b31036bcf2ce6792f8ce3c0d MD5 | raw file
  1/*
  2 *  This file is part of YaBS.
  3 *
  4 *      YaBS is free software: you can redistribute it and/or modify
  5 *      it under the terms of the GNU General Public License as published by
  6 *      the Free Software Foundation, either version 3 of the License, or
  7 *      (at your option) any later version.
  8 *
  9 *      YaBS is distributed in the hope that it will be useful,
 10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *      GNU General Public License for more details.
 13 *
 14 *      You should have received a copy of the GNU General Public License
 15 *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 16 */
 17package mpv5.ui.popups;
 18
 19import java.awt.event.ActionEvent;
 20import java.awt.event.ActionListener;
 21import java.awt.event.MouseAdapter;
 22import java.awt.event.MouseEvent;
 23import java.awt.event.MouseListener;
 24import java.io.File;
 25import java.util.logging.Level;
 26import java.util.logging.Logger;
 27import javax.swing.JMenuItem;
 28import javax.swing.JPopupMenu;
 29import javax.swing.JTable;
 30import mpv5.db.common.Context;
 31import mpv5.db.common.QueryHandler;
 32import mpv5.globals.Messages;
 33import mpv5.ui.frames.MPView;
 34import mpv5.ui.panels.DataPanel;
 35import mpv5.utils.files.FileDirectoryHandler;
 36
 37/**
 38 *
 39 *  
 40 */
 41public class FileTablePopUp extends JPopupMenu {
 42
 43    private static final long serialVersionUID = 1L;
 44    private static MouseListener pop;
 45    private static FileTablePopUp p;
 46
 47    /**
 48     *
 49     * @param t
 50     * @return
 51     */
 52    public static FileTablePopUp instanceOf(final JTable t) {
 53        if (p == null) {
 54            p = FileTablePopUp.getDefaultPopupMenu(t);
 55        }
 56        return p;
 57    }
 58
 59    private static FileTablePopUp getDefaultPopupMenu(final JTable dataTable) {
 60        FileTablePopUp t = new FileTablePopUp();
 61        t.addDefaultPopupMenu(dataTable);
 62        return t;
 63    }
 64
 65    /**
 66     * Adds a popup menu to the given table
 67     * @param items
 68     */
 69    public FileTablePopUp(JMenuItem[] items) {
 70        super();
 71
 72        for (int i = 0; i < items.length; i++) {
 73            JMenuItem item = items[i];
 74            item.setHorizontalTextPosition(JMenuItem.LEFT);
 75            this.add(item);
 76        }
 77    }
 78
 79    /**
 80     * Adds a Popup menu with default actions (delete, open, edit) to the given table
 81     * 
 82     * @param dataTable A table holding File Objects, first column MUST be the ID, <br/>
 83     * second column the desired file name
 84     */
 85    public void addDefaultPopupMenu(final JTable dataTable) {
 86
 87        JMenuItem x = new JMenuItem(Messages.SAVE_AS.getValue());
 88        x.addActionListener(new ActionListener() {
 89
 90            @Override
 91            public void actionPerformed(ActionEvent e) {
 92
 93                FileDirectoryHandler.save(QueryHandler.instanceOf().clone(Context.getFiles()).
 94                        retrieveFile(dataTable.getModel().getValueAt(dataTable.getSelectedRow(), 0).
 95                        toString(), new File(FileDirectoryHandler.getTempDir() + dataTable.getModel().
 96                        getValueAt(dataTable.getSelectedRow(), 1).toString())));
 97            }
 98        });
 99
100        JMenuItem i = new JMenuItem(Messages.ACTION_OPEN.getValue());
101        i.addActionListener(new ActionListener() {
102
103            @Override
104            public void actionPerformed(ActionEvent e) {
105
106                FileDirectoryHandler.open(QueryHandler.instanceOf().clone(Context.getFiles()).
107                        retrieveFile(dataTable.getModel().getValueAt(dataTable.getSelectedRow(), 0).
108                        toString(), new File(FileDirectoryHandler.getTempDir() + dataTable.getModel().
109                        getValueAt(dataTable.getSelectedRow(), 1).toString())));
110            }
111        });
112
113        JMenuItem h = new JMenuItem(Messages.ACTION_EDIT.getValue());
114        h.addActionListener(new ActionListener() {
115
116            @Override
117            public void actionPerformed(ActionEvent e) {
118
119                FileDirectoryHandler.edit(QueryHandler.instanceOf().clone(Context.getFiles()).
120                        retrieveFile(dataTable.getModel().getValueAt(dataTable.getSelectedRow(), 0).
121                        toString(), new File(FileDirectoryHandler.getTempDir() + dataTable.getModel().
122                        getValueAt(dataTable.getSelectedRow(), 1).toString())));
123            }
124        });
125
126        JMenuItem g = new JMenuItem(Messages.ACTION_DELETE.getValue());
127        g.addActionListener(new ActionListener() {
128
129            @Override
130            public void actionPerformed(ActionEvent e) {
131
132                try {
133                    QueryHandler.instanceOf().clone(Context.getFiles()).removeFile(dataTable.getModel().getValueAt(dataTable.getSelectedRow(), 0).toString());
134
135                    ((DataPanel) mpv5.YabsViewProxy.instance().getCurrentTab()).refresh();
136                } catch (Exception ex) {
137                    mpv5.logging.Log.Debug(ex);//Logger.getLogger(FileTablePopUp.class.getName()).log(Level.SEVERE, null, ex);
138                }
139            }
140        });
141
142        this.add(x);
143        this.add(i);
144        this.add(h);
145        this.add(g);
146    }
147
148    /**
149     * Remove the previously added Popup Mouselistener, if any
150     * @param to
151     */
152    public static void clear(final JTable to) {
153        to.removeMouseListener(pop);
154    }
155
156    /**
157     * Create a new, empty popup
158     */
159    public FileTablePopUp() {
160        super();
161    }
162
163    class MousePopupListener extends MouseAdapter {
164
165        @Override
166        public void mouseReleased(MouseEvent e) {
167            if (e.isPopupTrigger()) {
168                JTable source = (JTable) e.getSource();
169                int row = source.rowAtPoint(e.getPoint());
170                int column = source.columnAtPoint(e.getPoint());
171
172                if (!source.isRowSelected(row)) {
173                    source.changeSelection(row, column, false, false);
174                }
175
176                show(source, e.getX(), e.getY());
177            }
178        }
179    }
180}