001 /* $Id: ExternalApplicationMenu.java,v 1.10 2006/10/12 11:41:42 ringler Exp $
002 * This class is part of the mp3dings application
003 * Copyright (C) 2006 Moritz Ringler
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019 package net.sourceforge.mp3dings.utils;
020
021 import java.awt.Component;
022 import java.awt.EventQueue;
023 import java.awt.event.ActionEvent;
024 import java.awt.event.ActionListener;
025 import java.io.File;
026 import java.util.ArrayList;
027 import java.util.Collections;
028 import java.util.Iterator;
029 import java.util.List;
030 import java.util.logging.Logger;
031 import java.util.prefs.BackingStoreException;
032 import java.util.prefs.PreferenceChangeEvent;
033 import java.util.prefs.PreferenceChangeListener;
034 import javax.swing.JMenu;
035 import javax.swing.JMenuItem;
036 import javax.swing.SwingUtilities;
037 import de.mospace.lang.ExceptionHandler;
038 import de.mospace.swing.MiniShell;
039 import net.sourceforge.mp3dings.Globals;
040 import net.sourceforge.mp3dings.TableContextMenu;
041
042 public abstract class ExternalApplicationMenu extends JMenu{
043 private static final Logger logger = Logger.getLogger(ExternalApplicationMenu.class.getPackage().getName());
044 private ExceptionHandler warningDialog;
045
046 private class Item extends JMenuItem{
047 ActionListener al = new ActionListener(){
048 public void actionPerformed(ActionEvent e){
049 Item menuitem = (Item) e.getSource();
050 try{
051 final ExternalApplication app = ExternalApplication.load(menuitem.id);
052 Runnable runWhenDone = (ExternalApplicationMenu.this.getParent() instanceof TableContextMenu)
053 ? new Runnable(){
054 public void run(){
055 ((TableContextMenu) ExternalApplicationMenu.this.getParent())
056 .setStatus(Globals.getMessage("EXT_APP_RETURNED",app.getName()));
057 }
058 }
059 : null;
060 int exporter =
061 (app.getCallMode() == ExternalApplication.PLAYLIST)
062 ? app.getExporter()
063 : -1;
064 if(app.needsMp3dingsConsole()){
065 getConsole().getShowAction().actionPerformed(e);
066 }
067 app.execute(getFiles(exporter),getConsole().getProcessIOPane(),
068 getExceptionHandler(), runWhenDone);
069 } catch (java.io.FileNotFoundException ex){
070 getExceptionHandler().handleException(ex);
071 }
072 }
073 };
074
075 public Item(String text, int id){
076 super(text);
077 this.id = id;
078 addActionListener(al);
079 }
080 public int id;
081 }
082
083 private ExceptionHandler getExceptionHandler(){
084 if (warningDialog == null){
085 Component parent = this.getParent();
086 if(parent != null && parent instanceof TableContextMenu){
087 parent = ((TableContextMenu) parent).getOwner();
088 }
089 warningDialog = new DialogExceptionHandler(
090 Globals.getString("EXT_APP_ERR"),
091 parent);
092 }
093 return warningDialog;
094 }
095
096 List item;
097 public ExternalApplicationMenu(String title){
098 super(title);
099 JMenuItem jmi;
100
101 String[] keys = new String[0];
102 try{
103 keys = ExternalApplication.getPrefNode().keys();
104 } catch (BackingStoreException ex){
105 logger.severe("Error loading external applications.");
106 logger.severe(ex.toString());
107 }
108 List id = new ArrayList();
109 for(int i=0; i<keys.length; i++){
110 try{
111 id.add(Integer.valueOf(keys[i]));
112 } catch (NumberFormatException ex){
113 logger.severe("Error loading external application " + keys[i]);
114 logger.severe(ex.getMessage());
115 }
116 }
117 Collections.sort(id);
118 for(Iterator it = id.iterator(); it.hasNext();){
119 int iid = ((Integer) it.next()).intValue();
120 ExternalApplication app = ExternalApplication.load(iid);
121 if(app != null){
122 jmi = new Item(app.getName(), iid);
123 add(jmi);
124 }
125 }
126 ExternalApplication.getPrefNode().addPreferenceChangeListener(
127 new PreferenceChangeListener(){
128 public void preferenceChange(PreferenceChangeEvent evt){
129 Runnable pchange = new PrefChangeRunnable(evt);
130 /* process the event on the AWT Event dispatching thread
131 * to not get into synchronization trouble */
132 if(EventQueue.isDispatchThread()){
133 (pchange).run();
134 } else {
135 SwingUtilities.invokeLater(pchange);
136 }
137 }
138 });
139 }
140
141 private class PrefChangeRunnable implements java.lang.Runnable{
142 private PreferenceChangeEvent evt;
143
144 public PrefChangeRunnable(PreferenceChangeEvent evt){
145 this.evt = evt;
146 }
147
148 public void run(){
149 String key = evt.getKey();
150 int id = 0;
151 try{
152 id = Integer.parseInt(key);
153 } catch (NumberFormatException ex){
154 System.err.println(ex.getMessage());
155 }
156
157 /* get position for adding new menu item */
158 int pos = getMenuPos(id);
159 boolean exists = (pos < getMenuComponentCount());
160 Component comp = (exists)? getMenuComponent(pos) : null;
161 exists = exists && (comp instanceof Item);
162 exists = exists && (((Item) comp).id == id);
163 /* remove existing menu item if necessary */
164 if(exists){
165 remove(comp);
166 }
167 if(evt.getNewValue() != null){
168 String name = ExternalApplication.load(id).getName();
169 Item tmp = new Item(name, id);
170 insert(tmp, pos);
171 }
172 }
173 }
174
175 private int getMenuPos(int id){
176 Component[] items = getMenuComponents();
177 int result = items.length;
178 for(int i=0,itemid=0; i < items.length; i++){
179 if((items[i] instanceof Item)){
180 itemid = ((Item) items[i]).id;
181 if (itemid == id){
182 result = i;
183 break;
184 }
185 }
186 }
187 return result;
188 }
189
190 public abstract File[] getFiles(int exporter);
191 public abstract MiniShell getConsole();
192 }