001: /**
002: * Miroslav Popov, Aug 5, 2005
003: */package org.enhydra.jawe.base.recentfiles;
004:
005: import java.awt.event.ActionEvent;
006: import java.awt.event.ActionListener;
007: import java.io.FileOutputStream;
008: import java.util.StringTokenizer;
009:
010: import javax.swing.JComponent;
011: import javax.swing.JMenu;
012: import javax.swing.JMenuItem;
013:
014: import org.enhydra.jawe.BarFactory;
015: import org.enhydra.jawe.JaWEComponent;
016: import org.enhydra.jawe.JaWEComponentView;
017: import org.enhydra.jawe.JaWEConstants;
018: import org.enhydra.jawe.JaWEManager;
019: import org.enhydra.shark.xpdl.XMLUtil;
020:
021: /**
022: * @author Miroslav Popov
023: *
024: */
025: public class RecentFilesMenu extends JMenu implements
026: JaWEComponentView, ActionListener {
027:
028: public static final String RFL_FILENAME = "/.rfl";
029:
030: public void configure() {
031: }
032:
033: protected RecentFilesManager controller;
034:
035: public RecentFilesMenu(RecentFilesManager controller) {
036: this .controller = controller;
037: }
038:
039: public void init() {
040: setText(controller.getSettings().getLanguageDependentString(
041: controller.getName() + BarFactory.LABEL_POSTFIX));
042: String rfl = XMLUtil.fileToString(JaWEConstants.JAWE_USER_HOME
043: + RFL_FILENAME);
044: if (rfl != null) {
045: for (StringTokenizer st = new StringTokenizer(rfl, "\n"); st
046: .hasMoreTokens();) {
047: addToRecentFiles(st.nextToken());
048: }
049: }
050: }
051:
052: public void addToRecentFiles(String filename) {
053: if (filename == null)
054: return;
055: JMenuItem mItem;
056: for (int i = 0; i < getItemCount(); ++i) {
057: mItem = (JMenuItem) getMenuComponent(i);
058: if (filename.equals(mItem.getText().substring(2))) {
059: remove(i);
060: }
061: }
062:
063: int recentFileListSize = 10;
064: if (getItemCount() == recentFileListSize) {
065: remove(recentFileListSize - 1);
066: }
067:
068: mItem = new JMenuItem("1 " + filename);
069: mItem.addActionListener(this );
070:
071: insert(mItem, 0);
072: // changing mnemonics to correspond to the ordinal number of items
073: for (int i = 0; i < getItemCount(); ++i) {
074: mItem = (JMenuItem) getMenuComponent(i);
075: String oldText = mItem.getText();
076: String ordNo = String.valueOf(i + 1);
077: String mnemonic = ordNo.substring(ordNo.length() - 1, ordNo
078: .length());
079: mItem.setText(mnemonic + " " + oldText.substring(2));
080: BarFactory.setMnemonic(mItem, mnemonic);
081: }
082: }
083:
084: public void saveRecentFiles() {
085: try {
086: String fileList = "";
087: for (int i = getItemCount(); i > 0;) {
088: JMenuItem mItem = (JMenuItem) getMenuComponent(--i);
089: fileList += mItem.getText().substring(2);
090: if (i > 0)
091: fileList += "\n";
092: }
093: FileOutputStream fos = new FileOutputStream(
094: JaWEConstants.JAWE_USER_HOME + RFL_FILENAME);
095: fos.write(fileList.getBytes(JaWEManager.getInstance()
096: .getJaWEController().getControllerSettings()
097: .getEncoding()));
098: // Write to file
099: fos.flush();
100: fos.close();
101: } catch (Exception ex) {
102: }
103: }
104:
105: public void actionPerformed(ActionEvent ae) {
106: String filename = ae.getActionCommand().substring(2);
107: if (JaWEManager.getInstance().getJaWEController()
108: .tryToClosePackage(null, false)) {
109: JaWEManager.getInstance().getJaWEController()
110: .openPackageFromFile(filename);
111: }
112: }
113:
114: public JaWEComponent getJaWEComponent() {
115: return controller;
116: }
117:
118: public JComponent getDisplay() {
119: return this;
120: }
121: }
|