use of com.codename1.ui.ComponentSelector.Filter in project CodenameOne by codenameone.
the class AbstractCN1Mojo method getArtifact.
protected Artifact getArtifact(String groupId, String artifactId) {
Artifact out = project.getArtifacts().stream().filter(art -> art.getArtifactId().equals(artifactId) && art.getGroupId().equals(groupId)).findFirst().orElse(null);
if (out != null)
return out;
out = pluginArtifacts.stream().filter(art -> art.getArtifactId().equals(artifactId) && art.getGroupId().equals(groupId)).findFirst().orElse(null);
return out;
}
use of com.codename1.ui.ComponentSelector.Filter in project CodenameOne by codenameone.
the class JavaSEPort method createSkinsMenu.
private JMenu createSkinsMenu(final JFrame frm, final JMenu menu) throws MalformedURLException {
JMenu m;
if (menu == null) {
m = new JMenu("Skins");
m.setDoubleBuffered(true);
} else {
m = menu;
m.removeAll();
}
final JMenu skinMenu = m;
Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
String skinNames = pref.get("skins", DEFAULT_SKINS);
if (skinNames != null) {
if (skinNames.length() < DEFAULT_SKINS.length()) {
skinNames = DEFAULT_SKINS;
}
ButtonGroup skinGroup = new ButtonGroup();
StringTokenizer tkn = new StringTokenizer(skinNames, ";");
while (tkn.hasMoreTokens()) {
final String current = tkn.nextToken();
String name = current;
if (current.contains(":")) {
try {
URL u = new URL(current);
File f = new File(u.getFile());
if (!f.exists()) {
continue;
}
name = f.getName();
} catch (Exception e) {
continue;
}
} else {
// remove the old builtin skins from the menu
if (current.startsWith("/") && !current.equals(DEFAULT_SKIN)) {
continue;
}
}
String d = System.getProperty("dskin");
JRadioButtonMenuItem i = new JRadioButtonMenuItem(name, name.equals(pref.get("skin", d)));
i.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (netMonitor != null) {
netMonitor.dispose();
netMonitor = null;
}
if (perfMonitor != null) {
perfMonitor.dispose();
perfMonitor = null;
}
Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
pref.putBoolean("desktopSkin", false);
String mainClass = System.getProperty("MainClass");
if (mainClass != null) {
pref.put("skin", current);
frm.dispose();
System.setProperty("reload.simulator", "true");
} else {
loadSkinFile(current, frm);
refreshSkin(frm);
}
}
});
skinGroup.add(i);
skinMenu.add(i);
}
}
JMenuItem dSkin = new JMenuItem("Desktop.skin");
dSkin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (netMonitor != null) {
netMonitor.dispose();
netMonitor = null;
}
if (perfMonitor != null) {
perfMonitor.dispose();
perfMonitor = null;
}
Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
pref.putBoolean("desktopSkin", true);
pref.putBoolean("uwpDesktopSkin", false);
String mainClass = System.getProperty("MainClass");
if (mainClass != null) {
deinitializeSync();
frm.dispose();
System.setProperty("reload.simulator", "true");
}
}
});
JMenuItem uwpSkin = new JMenuItem("UWP Desktop.skin");
uwpSkin.setToolTipText("Windows 10 Desktop Skin");
uwpSkin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (netMonitor != null) {
netMonitor.dispose();
netMonitor = null;
}
if (perfMonitor != null) {
perfMonitor.dispose();
perfMonitor = null;
}
Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
pref.putBoolean("desktopSkin", true);
pref.putBoolean("uwpDesktopSkin", true);
String mainClass = System.getProperty("MainClass");
if (mainClass != null) {
deinitializeSync();
frm.dispose();
System.setProperty("reload.simulator", "true");
}
}
});
skinMenu.addSeparator();
skinMenu.add(dSkin);
skinMenu.add(uwpSkin);
skinMenu.addSeparator();
JMenuItem more = new JMenuItem("More...");
skinMenu.add(more);
more.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final JDialog pleaseWait = new JDialog(frm, false);
pleaseWait.setLocationRelativeTo(frm);
pleaseWait.setTitle("Message");
pleaseWait.setLayout(new BorderLayout());
pleaseWait.add(new JLabel(" Please Wait... "), BorderLayout.CENTER);
pleaseWait.pack();
pleaseWait.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JDialog d = new JDialog(frm, true);
d.setLocationRelativeTo(frm);
d.setTitle("Skins");
d.getContentPane().setLayout(new BorderLayout());
String userDir = System.getProperty("user.home");
final File skinDir = new File(userDir + "/.codenameone/");
if (!skinDir.exists()) {
skinDir.mkdir();
}
Vector data = new Vector();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
final Document[] doc = new Document[1];
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
InputStream is = openSkinsURL();
doc[0] = db.parse(is);
NodeList skins = doc[0].getElementsByTagName("Skin");
for (int i = 0; i < skins.getLength(); i++) {
Node skin = skins.item(i);
NamedNodeMap attr = skin.getAttributes();
String url = attr.getNamedItem("url").getNodeValue();
int ver = 0;
Node n = attr.getNamedItem("version");
if (n != null) {
ver = Integer.parseInt(n.getNodeValue());
}
boolean exists = new File(skinDir.getAbsolutePath() + url).exists();
if (!(exists) || Integer.parseInt(pref.get(url, "0")) < ver) {
Vector row = new Vector();
row.add(new Boolean(false));
row.add(new ImageIcon(new URL(defaultCodenameOneComProtocol + "://www.codenameone.com/OTA" + attr.getNamedItem("icon").getNodeValue())));
row.add(attr.getNamedItem("name").getNodeValue());
if (exists) {
row.add("Update");
} else {
row.add("New");
}
data.add(row);
}
}
} catch (Exception ex) {
Logger.getLogger(JavaSEPort.class.getName()).log(Level.SEVERE, null, ex);
}
if (data.size() == 0) {
pleaseWait.setVisible(false);
JOptionPane.showMessageDialog(frm, "No New Skins to Install");
return;
}
Vector cols = new Vector();
cols.add("Install");
cols.add("Icon");
cols.add("Name");
cols.add("");
final DefaultTableModel tableModel = new DefaultTableModel(data, cols) {
@Override
public boolean isCellEditable(int row, int column) {
return column == 0;
}
};
JTable skinsTable = new JTable(tableModel) {
@Override
public Class<?> getColumnClass(int column) {
if (column == 0) {
return Boolean.class;
}
if (column == 1) {
return Icon.class;
}
return super.getColumnClass(column);
}
};
skinsTable.setRowHeight(112);
skinsTable.getTableHeader().setReorderingAllowed(false);
final JTextField filter = new JTextField();
final TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(((DefaultTableModel) skinsTable.getModel()));
filter.getDocument().addDocumentListener(new javax.swing.event.DocumentListener() {
private void updateFilter() {
try {
RowFilter rf = RowFilter.regexFilter("(?i)" + filter.getText(), 2);
sorter.setRowFilter(rf);
} catch (java.util.regex.PatternSyntaxException e) {
return;
}
}
@Override
public void insertUpdate(javax.swing.event.DocumentEvent e) {
updateFilter();
}
@Override
public void removeUpdate(DocumentEvent e) {
updateFilter();
}
@Override
public void changedUpdate(DocumentEvent e) {
updateFilter();
}
});
skinsTable.setRowSorter(sorter);
d.getContentPane().add(filter, BorderLayout.NORTH);
d.getContentPane().add(new JScrollPane(skinsTable), BorderLayout.CENTER);
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton download = new JButton("Download");
download.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final Vector toDowload = new Vector();
NodeList skins = doc[0].getElementsByTagName("Skin");
for (int i = 0; i < tableModel.getRowCount(); i++) {
if (((Boolean) tableModel.getValueAt(i, 0)).booleanValue()) {
Node skin;
for (int j = 0; j < skins.getLength(); j++) {
skin = skins.item(j);
NamedNodeMap attr = skin.getAttributes();
if (attr.getNamedItem("name").getNodeValue().equals(tableModel.getValueAt(i, 2))) {
String url = attr.getNamedItem("url").getNodeValue();
String[] data = new String[2];
data[0] = defaultCodenameOneComProtocol + "://www.codenameone.com/OTA" + url;
data[1] = attr.getNamedItem("version").getNodeValue();
toDowload.add(data);
break;
}
}
}
}
if (toDowload.size() > 0) {
final JDialog downloadMessage = new JDialog(d, true);
downloadMessage.setTitle("Downloading");
downloadMessage.setLayout(new FlowLayout());
downloadMessage.setLocationRelativeTo(d);
final JLabel details = new JLabel("<br><br>Details");
downloadMessage.add(details);
final JLabel progress = new JLabel("Progress<br><br>");
downloadMessage.add(progress);
new Thread() {
@Override
public void run() {
for (Iterator it = toDowload.iterator(); it.hasNext(); ) {
String[] data = (String[]) it.next();
String url = data[0];
details.setText(url.substring(url.lastIndexOf("/")));
details.repaint();
progress.setText("");
progress.repaint();
try {
File skin = downloadSkin(skinDir, url, data[1], progress);
if (skin.exists()) {
addSkinName(skin.toURI().toString());
}
} catch (Exception e) {
}
}
downloadMessage.setVisible(false);
d.setVisible(false);
try {
createSkinsMenu(frm, skinMenu);
} catch (MalformedURLException ex) {
Logger.getLogger(JavaSEPort.class.getName()).log(Level.SEVERE, null, ex);
}
}
}.start();
downloadMessage.pack();
downloadMessage.setSize(200, 70);
downloadMessage.setVisible(true);
} else {
JOptionPane.showMessageDialog(d, "Choose a Skin to Download");
}
}
});
p.add(download);
d.getContentPane().add(p, BorderLayout.SOUTH);
d.pack();
pleaseWait.dispose();
d.setVisible(true);
}
});
}
});
skinMenu.addSeparator();
JMenuItem addSkin = new JMenuItem("Add New...");
skinMenu.add(addSkin);
addSkin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
FileDialog picker = new FileDialog(frm, "Add Skin");
picker.setMode(FileDialog.LOAD);
picker.setFilenameFilter(new FilenameFilter() {
public boolean accept(File file, String string) {
return string.endsWith(".skin");
}
});
picker.setModal(true);
picker.setVisible(true);
String file = picker.getFile();
if (file != null) {
if (netMonitor != null) {
netMonitor.dispose();
netMonitor = null;
}
if (perfMonitor != null) {
perfMonitor.dispose();
perfMonitor = null;
}
String mainClass = System.getProperty("MainClass");
if (mainClass != null) {
Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
pref.put("skin", picker.getDirectory() + File.separator + file);
deinitializeSync();
frm.dispose();
System.setProperty("reload.simulator", "true");
} else {
loadSkinFile(picker.getDirectory() + File.separator + file, frm);
refreshSkin(frm);
}
}
}
});
skinMenu.addSeparator();
JMenuItem reset = new JMenuItem("Reset Skins");
skinMenu.add(reset);
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (JOptionPane.showConfirmDialog(frm, "Are you sure you want to reset skins to default?", "Clean Storage", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
pref.put("skins", DEFAULT_SKINS);
String userDir = System.getProperty("user.home");
final File skinDir = new File(userDir + "/.codenameone/");
if (skinDir.exists()) {
File[] childs = skinDir.listFiles();
for (int i = 0; i < childs.length; i++) {
File child = childs[i];
if (child.getName().endsWith(".skin")) {
child.delete();
}
}
}
if (netMonitor != null) {
netMonitor.dispose();
netMonitor = null;
}
if (perfMonitor != null) {
perfMonitor.dispose();
perfMonitor = null;
}
String mainClass = System.getProperty("MainClass");
if (mainClass != null) {
pref.put("skin", DEFAULT_SKIN);
deinitializeSync();
frm.dispose();
System.setProperty("reload.simulator", "true");
} else {
loadSkinFile(DEFAULT_SKIN, frm);
refreshSkin(frm);
}
}
}
});
return skinMenu;
}
use of com.codename1.ui.ComponentSelector.Filter in project CodenameOne by codenameone.
the class AutocompleteOverrideFilterSample method showForm.
public void showForm() {
Form hi = new Form("Auto Complete", new BoxLayout(BoxLayout.Y_AXIS));
final DefaultListModel<String> options = new DefaultListModel<>();
AutoCompleteTextField ac = new AutoCompleteTextField(options) {
@Override
protected boolean filter(String text) {
if (text.length() == 0) {
return false;
}
String[] l = searchLocations(text);
if (l == null || l.length == 0) {
return false;
}
options.removeAll();
for (String s : l) {
options.addItem(s);
}
return true;
}
};
ac.setMinimumElementsShownInPopup(5);
hi.add(ac);
hi.add(new SpanLabel("This demo requires a valid google API key to be set below " + "you can get this key for the webservice (not the native key) by following the instructions here: " + "https://developers.google.com/places/web-service/get-api-key"));
hi.add(apiKey);
hi.getToolbar().addCommandToRightBar("Get Key", null, e -> Display.getInstance().execute("https://developers.google.com/places/web-service/get-api-key"));
hi.show();
}
use of com.codename1.ui.ComponentSelector.Filter in project CodenameOne by codenameone.
the class ComponentSelector method filter.
/**
* Filters the current found set against the given selector.
* @param selector The selector to filter the found set on.
* @return A new set of elements matching the selector.
*/
public ComponentSelector filter(String selector) {
ComponentSelector matcher = new ComponentSelector(selector, new Label());
LinkedHashSet<Component> matches = new LinkedHashSet<Component>();
for (Component c : this) {
if (matcher.match(c)) {
matches.add(c);
}
}
return matcher.addAll(matches, true);
}
use of com.codename1.ui.ComponentSelector.Filter in project CodenameOne by codenameone.
the class TestComponent method testSideMenuCloseNPE.
// Test to reproduce NPE reported here:
// https://stackoverflow.com/questions/47141271/codename1-nullpointerexception-after-update
// Initial fix did not fix case where global toolbar is false.
// https://github.com/codenameone/CodenameOne/commit/2c7ad31b9542dd8bbff2ab00c5a4b0728efa9d0c
//
private void testSideMenuCloseNPE(boolean globalToolbar, boolean onTopSideMenu) {
System.out.println("Testing SideMenuNPE with globalToolbar=" + globalToolbar + "; onTopSideMenu=" + onTopSideMenu);
boolean globalToolbarSetting = Toolbar.isGlobalToolbar();
boolean onTopSideMenuSetting = Toolbar.isOnTopSideMenu();
int commandBehaviour = Display.getInstance().getCommandBehavior();
Toolbar.setGlobalToolbar(globalToolbar);
Toolbar.setOnTopSideMenu(onTopSideMenu);
Display.getInstance().setCommandBehavior(Display.COMMAND_BEHAVIOR_SIDE_NAVIGATION);
try {
Form reset = new Form("Reset");
reset.setName("Reset");
reset.show();
TestUtils.waitForFormName("Reset");
Form home = new Form("Home");
home.setName("Home");
Toolbar homeTb = new Toolbar();
// if (!globalToolbar){
home.setToolbar(homeTb);
TestUtils.assertEqual(homeTb, home.getToolbar(), "getToolbar() returned wrong toolbar");
// }
Form form1 = new Form("Form1");
form1.setName("Form1");
form1.setToolbar(new Toolbar());
// Add navigation commands to the home Form
NavigationCommand homeCommand = new NavigationCommand("Home");
homeCommand.setNextForm(home);
home.getToolbar().addCommandToSideMenu(homeCommand);
NavigationCommand cmd1 = new NavigationCommand("Form1");
cmd1.setNextForm(form1);
home.getToolbar().addCommandToSideMenu(cmd1);
home.show();
TestUtils.waitForFormName("Home");
home.getToolbar().openSideMenu();
// wait for side menu to open
TestUtils.waitFor(500);
Button cmd1Btn;
if (Toolbar.isOnTopSideMenu()) {
cmd1Btn = $("SideCommand", home).filter(c -> {
return "Form1".equals(((Button) c).getText());
}).asComponent(Button.class);
} else {
System.out.println(home.getToolbar().getMenuBar());
cmd1Btn = $("SideCommand", home.getToolbar().getMenuBar()).filter(c -> {
return "Form1".equals(((Button) c).getText());
}).asComponent(Button.class);
}
Button c = cmd1Btn;
System.out.println(c);
int actualX = c.getAbsoluteX() + (int) (0.5f * c.getWidth());
int actualY = c.getAbsoluteY() + (int) (0.5f * c.getHeight());
Display.getInstance().getCurrent().pointerPressed(actualX, actualY);
Display.getInstance().getCurrent().pointerReleased(actualX, actualY);
TestUtils.waitForFormName("Form1");
// home.getToolbar().getMenuBar().actionPerformed(new ActionEvent(home));
// home.getToolbar().sho
} finally {
Toolbar.setGlobalToolbar(globalToolbarSetting);
Display.getInstance().setCommandBehavior(commandBehaviour);
Toolbar.setOnTopSideMenu(onTopSideMenuSetting);
}
}
Aggregations