1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-13 07:54:20 +01:00
Arduino/app/src/cc/arduino/contributions/ui/InstallerJDialog.java

292 lines
9.8 KiB
Java
Raw Normal View History

2014-09-01 10:11:12 +02:00
/*
* This file is part of Arduino.
*
2015-03-11 16:32:04 +01:00
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
2014-09-01 10:11:12 +02:00
*
* Arduino is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*/
package cc.arduino.contributions.ui;
2014-09-01 10:11:12 +02:00
import cc.arduino.contributions.ui.listeners.AbstractKeyListener;
import com.google.common.base.Predicate;
import processing.app.Base;
import processing.app.Theme;
2014-09-01 10:11:12 +02:00
import javax.swing.*;
2014-09-01 10:11:12 +02:00
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
2014-09-01 10:11:12 +02:00
import static cc.arduino.contributions.packages.ui.ContributionIndexTableModel.DESCRIPTION_COL;
import static processing.app.I18n._;
2014-09-01 10:11:12 +02:00
public abstract class InstallerJDialog<T> extends JDialog {
2014-09-01 10:11:12 +02:00
// Toolbar on top of the window:
// - Categories drop-down menu
protected JLabel categoryLabel;
protected JComboBox categoryChooser;
protected Component categoryStrut1;
protected Component categoryStrut2;
protected Component categoryStrut3;
// - Search text-field
protected FilterJTextField filterField;
// Currently selected category and filters
protected Predicate<T> categoryFilter;
2014-09-01 10:11:12 +02:00
protected String[] filters;
protected final String noConnectionErrorMessage;
2014-09-01 10:11:12 +02:00
// Real contribution table
protected JTable contribTable;
// Model behind the table
protected FilteredAbstractTableModel contribModel;
abstract protected FilteredAbstractTableModel createContribModel();
abstract protected InstallerTableCell createCellRenderer();
abstract protected InstallerTableCell createCellEditor();
// Bottom:
// - Progress bar
protected ProgressJProgressBar progressBar;
protected Box progressBox;
protected Box errorMessageBox;
private final JLabel errorMessage;
2014-09-01 10:11:12 +02:00
public InstallerJDialog(Frame parent, String title, ModalityType applicationModal, String noConnectionErrorMessage) {
2014-09-01 10:11:12 +02:00
super(parent, title, applicationModal);
this.noConnectionErrorMessage = noConnectionErrorMessage;
2014-09-01 10:11:12 +02:00
setResizable(true);
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
{
categoryStrut1 = Box.createHorizontalStrut(5);
categoryStrut2 = Box.createHorizontalStrut(5);
categoryStrut3 = Box.createHorizontalStrut(5);
categoryLabel = new JLabel(_("Category:"));
categoryChooser = new JComboBox();
categoryChooser.setMaximumRowCount(20);
categoryChooser.setEnabled(false);
filterField = new FilterJTextField(_("Filter your search...")) {
@Override
protected void onFilter(String[] _filters) {
filters = _filters;
if (contribTable.getCellEditor() != null) {
contribTable.getCellEditor().stopCellEditing();
}
contribModel.updateIndexFilter(categoryFilter, filters);
2014-09-01 10:11:12 +02:00
}
};
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(categoryStrut1);
panel.add(categoryLabel);
panel.add(categoryStrut2);
panel.add(categoryChooser);
panel.add(categoryStrut3);
panel.add(filterField);
panel.setBorder(new EmptyBorder(7, 7, 7, 7));
pane.add(panel, BorderLayout.NORTH);
}
contribModel = createContribModel();
contribTable = new JTable(contribModel);
contribTable.setTableHeader(null);
contribTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
contribTable.setColumnSelectionAllowed(false);
contribTable.setDragEnabled(false);
contribTable.setIntercellSpacing(new Dimension(0, 1));
contribTable.setShowVerticalLines(false);
contribTable.setSelectionBackground(Theme.getColor("status.notice.bgcolor"));
contribTable.addKeyListener(new AbstractKeyListener() {
@Override
public void keyReleased(KeyEvent keyEvent) {
if (keyEvent.getKeyCode() != keyEvent.VK_DOWN && keyEvent.getKeyCode() != KeyEvent.VK_UP) {
return;
}
contribTable.editCellAt(contribTable.getSelectedRow(), contribTable.getSelectedColumn());
}
});
2014-09-01 10:11:12 +02:00
{
TableColumnModel tcm = contribTable.getColumnModel();
TableColumn col = tcm.getColumn(DESCRIPTION_COL);
col.setCellRenderer(createCellRenderer());
col.setCellEditor(createCellEditor());
2014-09-01 10:11:12 +02:00
col.setResizable(true);
}
{
2015-03-23 11:10:12 +01:00
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(contribTable);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.getVerticalScrollBar().setUnitIncrement(7);
pane.add(scrollPane, BorderLayout.CENTER);
2014-09-01 10:11:12 +02:00
}
pane.add(Box.createHorizontalStrut(10), BorderLayout.WEST);
pane.add(Box.createHorizontalStrut(10), BorderLayout.EAST);
progressBar = new ProgressJProgressBar();
progressBar.setStringPainted(true);
progressBar.setString(" ");
progressBar.setVisible(true);
errorMessage = new JLabel("");
errorMessage.setForeground(Color.RED);
2014-09-01 10:11:12 +02:00
{
JButton cancelButton = new JButton(_("Cancel"));
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
onCancelPressed();
}
});
progressBox = Box.createHorizontalBox();
progressBox.add(progressBar);
progressBox.add(Box.createHorizontalStrut(5));
progressBox.add(cancelButton);
JButton dismissErrorMessageButton = new JButton(_("OK"));
dismissErrorMessageButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
clearErrorMessage();
}
});
errorMessageBox = Box.createHorizontalBox();
errorMessageBox.add(Box.createHorizontalGlue());
errorMessageBox.add(errorMessage);
errorMessageBox.add(Box.createHorizontalGlue());
errorMessageBox.add(dismissErrorMessageButton);
errorMessageBox.setVisible(false);
2014-09-01 10:11:12 +02:00
}
{
JPanel progressPanel = new JPanel();
progressPanel.setBorder(new EmptyBorder(7, 7, 7, 7));
progressPanel.setLayout(new BoxLayout(progressPanel, BoxLayout.Y_AXIS));
progressPanel.add(progressBox);
progressPanel.add(errorMessageBox);
2014-09-01 10:11:12 +02:00
pane.add(progressPanel, BorderLayout.SOUTH);
}
2015-02-25 13:08:40 +01:00
setProgressVisible(false, "");
2014-09-01 10:11:12 +02:00
setMinimumSize(new Dimension(600, 450));
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Base.registerWindowCloseKeys(getRootPane(), new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
InstallerJDialog.this.dispatchEvent(new WindowEvent(InstallerJDialog.this, WindowEvent.WINDOW_CLOSING));
}
});
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
onUpdatePressed();
}
});
2014-09-01 10:11:12 +02:00
}
public void setErrorMessage(String message) {
errorMessage.setText("<html><body>" + message + "</body></html>");
errorMessageBox.setVisible(true);
}
public void clearErrorMessage() {
errorMessage.setText("");
errorMessageBox.setVisible(false);
}
2015-02-25 13:08:40 +01:00
public void setProgressVisible(boolean visible, String status) {
if (visible) {
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
} else {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
2014-09-01 10:11:12 +02:00
progressBox.setVisible(visible);
filterField.setEnabled(!visible);
categoryChooser.setEnabled(!visible);
contribTable.setEnabled(!visible);
errorMessageBox.setVisible(false);
if (contribTable.getCellEditor() != null) {
((InstallerTableCell) contribTable.getCellEditor()).setEnabled(!visible);
((InstallerTableCell) contribTable.getCellEditor()).setStatus(status);
}
2014-09-01 10:11:12 +02:00
}
protected ActionListener categoryChooserActionListener = new ActionListener() {
2015-03-09 09:59:35 +01:00
2014-09-01 10:11:12 +02:00
@Override
2015-03-09 09:59:35 +01:00
public void actionPerformed(ActionEvent event) {
DropdownItem<T> selected = (DropdownItem<T>) categoryChooser.getSelectedItem();
if (categoryFilter == null || !categoryFilter.equals(selected)) {
categoryFilter = selected.getFilterPredicate();
if (contribTable.getCellEditor() != null) {
contribTable.getCellEditor().stopCellEditing();
}
contribModel.updateIndexFilter(categoryFilter, filters);
2014-09-01 10:11:12 +02:00
}
}
};
/**
* Action performed when the Cancel button is pressed.
*/
protected void onCancelPressed() {
clearErrorMessage();
2014-09-01 10:11:12 +02:00
}
/**
* Action performed when the "Update List" button is pressed.
*/
protected void onUpdatePressed() {
clearErrorMessage();
2014-09-01 10:11:12 +02:00
}
}