Example #1
0
  public void createUI() {
    List<String> features = features = ExtractorManager.getSupportedFeatures();
    songs = DBHandler.getInstance().getSongs(null);
    final LTableModel model = new LTableModel(features, songs);
    model.setCurrentFolder(null);
    JTable table = new JTable();
    JScrollPane scroller = new JScrollPane(table);
    scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    table.setModel(model);
    final Border eBorder = new EmptyBorder(5, 2, 5, 2);
    table.setDefaultRenderer(
        File.class,
        new TableCellRenderer() {

          @Override
          public Component getTableCellRendererComponent(
              JTable table,
              Object value,
              boolean isSelected,
              boolean hasFocus,
              int row,
              int column) {
            JLabel label;
            if (value instanceof File) {
              String name = ((File) value).getName();
              if (!Utils.stringNotNullOrEmpty(name)) {
                name = ((File) value).getAbsolutePath();
              }
              label =
                  new JLabel(
                      name,
                      Utils.getIcon(
                          ((File) value).isDirectory()
                              ? ResourceConstants.FOLDER_ICON
                              : ResourceConstants.SONG_ICON),
                      SwingConstants.LEFT);
              label.setBorder(eBorder);
              if (((File) value).isFile()) {
                int index = songs.indexOf(new Song((File) value));
                if (index < 0) {
                  // not indexed;
                  label.setForeground(Color.GRAY);
                }
              }
              return label;
            }
            label = new JLabel(value.toString());
            return label;
          }
        });

    JTextField txtField = new JTextField();
    AutoCompletionService completionService = new AutoCompletionService();
    model.setCompletionService(completionService);
    completionService.createMapFromSongList(DBHandler.getInstance().getAllSongs());
    table.setDefaultEditor(String.class, new LCellEditor(txtField, completionService, features));

    table.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() >= 2) {
              JTable target = (JTable) e.getSource();
              int row = target.getSelectedRow();
              LTableModel lModel = (LTableModel) target.getModel();
              File file = (File) lModel.getValueAt(row, 0);
              if (file.getName().equalsIgnoreCase("..")) {
                if (lModel.getCurrentFolder() != null) {
                  lModel.setCurrentFolder(lModel.getCurrentFolder().getParentFile());
                }
              } else if (file.isDirectory()) {
                lModel.setCurrentFolder(file);
              }
            }
          }
        });
    TableColumnModel cModel = table.getColumnModel();
    cModel.getColumn(0).setMinWidth(100);

    table.setRowHeight(ROW_HEIGHT);

    JButton btnCommit =
        new JButton(
            new AbstractAction("Commit Changes") {

              @Override
              public void actionPerformed(ActionEvent arg0) {
                final List<Modification> changes = model.getChanges();
                final ProgressMonitor monitor =
                    new ProgressMonitor(
                        MetaDataEditor.this,
                        "Committing Changes",
                        "Writing the changes to the DB",
                        0,
                        changes.size());
                new Thread() {
                  @Override
                  public void run() {
                    DBHandler.getInstance().applyModifications(changes, monitor);
                    cleanChanges();
                    MetaDataEditor.this.setVisible(false);
                  }
                }.start();
              }
            });

    JButton btnCancel =
        new JButton(
            new AbstractAction("Cancel") {

              @Override
              public void actionPerformed(ActionEvent arg0) {
                disposeChanges();
                MetaDataEditor.this.setVisible(false);
              }
            });

    JLabel lblHelp =
        new JLabel(
            "<html>You can set a value to all songs in a folder by specifing the value at the folder level. By default, it will not overwrite existing values. To overwrite, tick the checkbox below</html>");
    // JLabel lblNote = new JLabel(
    // "Note: Once the value is set for all songs recursively, the value entered at the folder level
    // will disappear.");
    chkOverwrite =
        new JCheckBox(
            new AbstractAction("Overwrite existing values if required") {

              @Override
              public void actionPerformed(ActionEvent e) {
                model.setDoOverwrite(chkOverwrite.isSelected());
              }
            });

    table.setPreferredScrollableViewportSize(new Dimension(1000, 500));
    JPanel panel = new JPanel(new MigLayout("", "[grow]", "[][][grow][]"));

    JPanel controlPanel = new JPanel(new MigLayout("", "push[][]", "[]"));
    controlPanel.add(btnCommit, "flowx,push,align right");
    controlPanel.add(btnCancel, "flowx");

    panel.add(lblHelp, "span");
    // panel.add(lblNote, "span");
    panel.add(chkOverwrite, "span");
    panel.add(scroller, "span");
    panel.add(controlPanel, "span,grow");
    this.setContentPane(panel);

    this.setVisible(true);
    this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
    this.pack();
  }
Example #2
0
 protected void disposeChanges() {
   songs = DBHandler.getInstance().getSongs(null);
 }