more work on the permission list, better selection setup

This commit is contained in:
benfry
2010-07-18 02:02:35 +00:00
parent b612583f40
commit 15c7a7f64d
@@ -6,9 +6,11 @@ import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import processing.app.Base;
import processing.app.Editor;
import processing.app.Preferences;
import processing.app.tools.Tool;
//import processing.core.*;
import processing.core.PApplet;
@@ -18,6 +20,232 @@ public class Permissions extends JFrame implements Tool {
static final String GUIDE_URL =
"http://developer.android.com/guide/topics/security/security.html#permissions";
static final int BORDER_HORIZ = 5;
static final int BORDER_VERT = 3;
JScrollPane permissionsScroller;
JList permissionList;
JLabel descriptionLabel;
// JTextArea descriptionLabel;
Editor editor;
public Permissions() {
//public Permissions(Editor editor) {
super("Android Permissions Selector");
// this.editor = editor;
// XMLElement xml =
permissionList = new CheckBoxList();
// permissionList.addMouseListener(new MouseAdapter() {
// public void mousePressed(MouseEvent e) {
// if (isEnabled()) {
// int index = permissionList.locationToIndex(e.getPoint());
// if (index == -1) {
// descriptionLabel.setText("");
// } else {
//// descriptionLabel.setText("<html>" + description[index] + "</html>");
// descriptionLabel.setText(description[index]);
// }
// }
// }
// });
// ListSelectionModel lsm = permissionList.getSelectionModel();
// lsm.addListSelectionListener(new ListSelectionListener() {
// public void valueChanged(ListSelectionEvent e) {
//// ListSelectionModel lsm = (ListSelectionModel) e.getSource();
// int index = e.getFirstIndex();
// if (index == -1) {
// descriptionLabel.setText("");
// } else {
// descriptionLabel.setText("<html>" + description[index] + "</html>");
//// descriptionLabel.setText(description[index]);
// }
// }
// });
permissionList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
int index = permissionList.getSelectedIndex();
if (index == -1) {
descriptionLabel.setText("");
} else {
descriptionLabel.setText("<html>" + description[index] + "</html>");
//descriptionLabel.setText(description[index]);
}
}
}
});
// permissionList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
// permissionList.setFixedCellWidth(300);
// int h = permissionList.getFixedCellHeight();
// permissionList.setFixedCellHeight(h + 8);
permissionList.setFixedCellHeight(20);
permissionList.setBorder(new EmptyBorder(BORDER_VERT, BORDER_HORIZ, BORDER_VERT, BORDER_HORIZ));
DefaultListModel model = new DefaultListModel();
permissionList.setModel(model);
for (String item : title) {
model.addElement(new JCheckBox(item));
}
permissionsScroller =
new JScrollPane(permissionList,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
// permissionList.setVisibleRowCount(20);
permissionList.setVisibleRowCount(12);
// permissionList.setPreferredSize(new Dimension(400, 300));
// permissionsScroller.setPreferredSize(new Dimension(400, 300));
permissionList.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == ' ') {
int index = permissionList.getSelectedIndex();
JCheckBox checkbox =
(JCheckBox) permissionList.getModel().getElementAt(index);
checkbox.setSelected(!checkbox.isSelected());
permissionList.repaint();
}
}
});
Container outer = getContentPane();
outer.setLayout(new BorderLayout());
JPanel pain = new JPanel();
pain.setBorder(new EmptyBorder(13, 13, 13, 13));
outer.add(pain, BorderLayout.CENTER);
pain.setLayout(new BoxLayout(pain, BoxLayout.Y_AXIS));
String labelText =
"<html>" +
"Android applications must specifically ask for permission\n" +
"to do things like connect to the internet, write a file,\n" +
"or make phone calls. When installing your application,\n" +
"users will be asked whether they want to allow such access.\n" +
"More about permissions can be found " +
"<a href=\"" + GUIDE_URL + "\">here</a>.</body></html>";
// "<html>" +
// "Android applications must specifically ask for permission\n" +
// "to do things like connect to the internet, write a file,\n" +
// "or make phone calls. When installing your application,\n" +
// "users will be asked whether they want to allow such access.\n" +
// "More about permissions can be found " +
// "<a href=\"" + GUIDE_URL + "\">here</a>.</body></html>";
// JTextArea textarea = new JTextArea(labelText);
// JTextArea textarea = new JTextArea(5, 40);
// textarea.setText(labelText);
JLabel textarea = new JLabel(labelText) {
public Dimension getPreferredSize() {
return new Dimension(400, 100);
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public Dimension getMaximumSize() {
return getPreferredSize();
}
};
// textarea.setBorder(new EmptyBorder(13, 8, 13, 8));
// textarea.setBackground(null);
// textarea.setBackground(Color.RED);
// textarea.setEditable(false);
// textarea.setHighlighter(null);
// textarea.setFont(new Font("Dialog", Font.PLAIN, 12));
pain.add(textarea);
// textarea.setForeground(Color.RED);
// pain.setBackground(Color.GREEN);
// permissionList.setEnabled(false);
pain.add(permissionsScroller);
pain.add(Box.createVerticalStrut(8));
// descriptionLabel = new JTextArea(4, 10);
descriptionLabel = new JLabel() {
public Dimension getPreferredSize() {
return new Dimension(400, 100);
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public Dimension getMaximumSize() {
return getPreferredSize();
}
};
descriptionLabel.setVerticalAlignment(JLabel.TOP);
pain.add(descriptionLabel);
pain.add(Box.createVerticalStrut(8));
JPanel buttons = new JPanel();
JButton okButton = new JButton("OK");
Dimension dim = new Dimension(Preferences.BUTTON_WIDTH,
okButton.getPreferredSize().height);
okButton.setPreferredSize(dim);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PApplet.println(getSelections());
setVisible(false);
}
});
okButton.setEnabled(true);
buttons.add(okButton);
pain.add(buttons);
JRootPane root = getRootPane();
root.setDefaultButton(okButton);
ActionListener disposer = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
setVisible(false);
}
};
Base.registerWindowCloseKeys(root, disposer);
Base.setIcon(this);
pack();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = getSize();
setLocation((screen.width - windowSize.width) / 2,
(screen.height - windowSize.height) / 2);
// setVisible(true);
}
protected String[] getSelections() {
ArrayList<String> sel = new ArrayList<String>();
DefaultListModel model = (DefaultListModel) permissionList.getModel();
for (int i = 0; i < count; i++) {
if (((JCheckBox) model.get(i)).isSelected()) {
sel.add(title[i]);
}
}
return sel.toArray(new String[0]);
}
public String getMenuTitle() {
return "Android Permissions";
}
public void init(Editor editor) {
this.editor = editor;
}
public void run() {
setVisible(true);
}
/**
* Created by inserting the HTML doc into OpenOffice, then copy and pasting
* the table into a plain text document, then adding the quotes via search
@@ -153,169 +381,6 @@ public class Permissions extends JFrame implements Tool {
description[i] = listing[i*2+1];
}
}
JScrollPane permissionsScroller;
JList permissionList;
JLabel descriptionLabel;
Editor editor;
public Permissions() {
//public Permissions(Editor editor) {
super("Android Permissions Selector");
// this.editor = editor;
// XMLElement xml =
permissionList = new CheckBoxList();
permissionList.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (isEnabled()) {
int index = permissionList.locationToIndex(e.getPoint());
if (index == -1) {
descriptionLabel.setText("");
} else {
descriptionLabel.setText("<html>" + description[index] + "</html>");
}
}
}
});
DefaultListModel model = new DefaultListModel();
permissionList.setModel(model);
for (String item : title) {
model.addElement(new JCheckBox(item));
}
permissionsScroller =
new JScrollPane(permissionList,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
Container outer = getContentPane();
outer.setLayout(new BorderLayout());
JPanel pain = new JPanel();
pain.setBorder(new EmptyBorder(13, 13, 13, 13));
outer.add(pain, BorderLayout.CENTER);
pain.setLayout(new BoxLayout(pain, BoxLayout.Y_AXIS));
String labelText =
"<html>" +
"Android applications must specifically ask for permission\n" +
"to do things like connect to the internet, write a file,\n" +
"or make phone calls. When installing your application,\n" +
"users will be asked whether they want to allow such access.\n" +
"More about permissions can be found " +
"<a href=\"" + GUIDE_URL + "\">here</a>.</body></html>";
// "<html>" +
// "Android applications must specifically ask for permission\n" +
// "to do things like connect to the internet, write a file,\n" +
// "or make phone calls. When installing your application,\n" +
// "users will be asked whether they want to allow such access.\n" +
// "More about permissions can be found " +
// "<a href=\"" + GUIDE_URL + "\">here</a>.</body></html>";
// JTextArea textarea = new JTextArea(labelText);
// JTextArea textarea = new JTextArea(5, 40);
// textarea.setText(labelText);
JLabel textarea = new JLabel(labelText) {
public Dimension getPreferredSize() {
return new Dimension(400, 200);
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public Dimension getMaximumSize() {
return getPreferredSize();
}
};
textarea.setBorder(new EmptyBorder(13, 8, 13, 8));
// textarea.setBackground(null);
textarea.setBackground(Color.RED);
// textarea.setEditable(false);
// textarea.setHighlighter(null);
textarea.setFont(new Font("Dialog", Font.PLAIN, 12));
pain.add(textarea);
// permissionList.setEnabled(false);
pain.add(permissionsScroller);
pain.add(Box.createVerticalStrut(8));
descriptionLabel = new JLabel(labelText) {
public Dimension getPreferredSize() {
return new Dimension(400, 200);
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
public Dimension getMaximumSize() {
return getPreferredSize();
}
};
pain.add(descriptionLabel);
pain.add(Box.createVerticalStrut(8));
JPanel buttons = new JPanel();
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PApplet.println(getSelections());
setVisible(false);
}
});
okButton.setEnabled(true);
buttons.add(okButton);
pain.add(buttons);
JRootPane root = getRootPane();
root.setDefaultButton(okButton);
ActionListener disposer = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
setVisible(false);
}
};
Base.registerWindowCloseKeys(root, disposer);
Base.setIcon(this);
pack();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = getSize();
setLocation((screen.width - windowSize.width) / 2,
(screen.height - windowSize.height) / 2);
// setVisible(true);
}
protected String[] getSelections() {
ArrayList<String> sel = new ArrayList<String>();
DefaultListModel model = (DefaultListModel) permissionList.getModel();
for (int i = 0; i < count; i++) {
if (((JCheckBox) model.get(i)).isSelected()) {
sel.add(title[i]);
}
}
return sel.toArray(new String[0]);
}
public String getMenuTitle() {
return "Android Permissions";
}
public void init(Editor editor) {
this.editor = editor;
}
public void run() {
setVisible(true);
}
}
@@ -334,6 +399,8 @@ class CheckBoxList extends JList {
// get the width of a checkbox so we can figure out if the mouse is inside
checkboxWidth = new JCheckBox().getPreferredSize().width;
// add the amount for the inset
checkboxWidth += Permissions.BORDER_HORIZ;
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
@@ -356,13 +423,14 @@ class CheckBoxList extends JList {
});
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
protected class CellRenderer implements ListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected,
boolean cellHasFocus) {
JCheckBox checkbox = (JCheckBox) value;
// checkbox.setBorder(new EmptyBorder(13, 5, 3, 5)); // trying again
checkbox.setBackground(isSelected ? getSelectionBackground() : getBackground());
checkbox.setForeground(isSelected ? getSelectionForeground() : getForeground());
//checkbox.setEnabled(isEnabled());