+ class RecentlyOpened extends JPanel
+ {
+ private static final long serialVersionUID = 1L;
+
+ JList<String> list;
+
+ RecentlyOpened()
+ {
+ setPreferredSize(new Dimension(300, 100));
+ String historyItems = Cache.getProperty("RECENT_FILE");
+ StringTokenizer st;
+ Vector<String> recent = new Vector<>();
+
+ if (historyItems != null)
+ {
+ st = new StringTokenizer(historyItems, "\t");
+
+ while (st.hasMoreTokens())
+ {
+ recent.addElement(st.nextToken());
+ }
+ }
+
+ list = new JList<>(recent);
+ list.setCellRenderer(new recentlyOpenedCellRenderer());
+
+ list.addMouseListener(new MouseAdapter()
+ {
+ @Override
+ public void mousePressed(MouseEvent evt)
+ {
+ recentListSelectionChanged(list.getSelectedValue());
+ }
+ });
+
+ TitledBorder recentlyOpenedBorder = new TitledBorder(
+ MessageManager.getString("label.recently_opened"));
+ recentlyOpenedBorder.setTitleFont(
+ recentlyOpenedBorder.getTitleFont().deriveFont(10f));
+ this.setBorder(recentlyOpenedBorder);
+
+ final JScrollPane scroller = new JScrollPane(list);
+
+ SpringLayout layout = new SpringLayout();
+ layout.putConstraint(SpringLayout.WEST, scroller, 5,
+ SpringLayout.WEST, this);
+ layout.putConstraint(SpringLayout.NORTH, scroller, 5,
+ SpringLayout.NORTH, this);
+
+ // one size okay for all
+ scroller.setPreferredSize(new Dimension(280, 105));
+ this.add(scroller);
+
+ SwingUtilities.invokeLater(new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ scroller.getHorizontalScrollBar()
+ .setValue(scroller.getHorizontalScrollBar().getMaximum());
+ }
+ });
+
+ }
+
+ }
+
+ class recentlyOpenedCellRenderer extends JLabel
+ implements ListCellRenderer<String>
+ {
+ private final static int maxChars = 46;
+
+ private final static String ellipsis = "...";
+
+ @Override
+ public Component getListCellRendererComponent(
+ JList<? extends String> list, String value, int index,
+ boolean isSelected, boolean cellHasFocus)
+ {
+ String filename = value.toString();
+ String displayFilename;
+ if (filename.length() > maxChars)
+ {
+ StringBuilder displayFileSB = new StringBuilder();
+ File file = new File(filename);
+ displayFileSB.append(file.getName());
+ if (file.getParent() != null)
+ {
+ File parent = file;
+ boolean spaceleft = true;
+ while (spaceleft && parent.getParent() != null)
+ {
+ parent = parent.getParentFile();
+ String name = parent.getName();
+ displayFileSB.insert(0, File.separator);
+ if (displayFileSB.length() + name.length() < maxChars - 1)
+ {
+ displayFileSB.insert(0, name);
+ }
+ else
+ {
+ displayFileSB.insert(0, ellipsis);
+ spaceleft = false;
+ }
+ }
+ if (spaceleft && filename.startsWith(File.separator)
+ && !(displayFileSB.charAt(0) == File.separatorChar))
+ {
+ displayFileSB.insert(0, File.separator);
+ }
+ }
+ displayFilename = displayFileSB.toString();
+ }
+ else
+ {
+ displayFilename = filename;
+ }
+ this.setText(displayFilename.toString());
+ this.setToolTipText(filename);
+ if (isSelected)
+ {
+ setBackground(list.getSelectionBackground());
+ setForeground(list.getSelectionForeground());
+ }
+ else
+ {
+ setBackground(list.getBackground());
+ setForeground(list.getForeground());
+ }
+ this.setHorizontalAlignment(SwingConstants.TRAILING);
+ this.setEnabled(list.isEnabled());
+ this.setFont(list.getFont().deriveFont(12f));
+ this.setOpaque(true);
+ return this;
+ }
+
+ }
+
+ /*
+ @Override
+ public JalviewFileChooser setResponseHandler(Object response,
+ Runnable action)
+ {
+ callbacks.put(response, new Callable<Void>()
+ {
+ @Override
+ public Void call()
+ {
+ action.run();
+ return null;
+ }
+ });
+ return this;
+ }
+ */