use of com.codename1.ui.layouts.BorderLayout in project CodenameOne by codenameone.
the class SEBrowserComponent method init.
private static void init(SEBrowserComponent self, BrowserComponent p) {
final WeakReference<SEBrowserComponent> weakSelf = new WeakReference<>(self);
final WeakReference<BrowserComponent> weakP = new WeakReference<>(p);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SEBrowserComponent self = weakSelf.get();
if (self == null) {
return;
}
self.cnt = new InternalJPanel(self.instance, self);
// <--- Important if container is opaque it will cause
self.cnt.setOpaque(false);
// all kinds of flicker due to painting conflicts with CN1 pipeline.
self.cnt.setLayout(new BorderLayout());
self.cnt.add(BorderLayout.CENTER, self.panel);
// cnt.setVisible(false);
}
});
self.web.getEngine().getLoadWorker().messageProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> ov, String t, String t1) {
SEBrowserComponent self = weakSelf.get();
BrowserComponent p = weakP.get();
if (self == null || p == null) {
return;
}
if (t1.startsWith("Loading http:") || t1.startsWith("Loading file:") || t1.startsWith("Loading https:")) {
String url = t1.substring("Loading ".length());
if (!url.equals(self.currentURL)) {
p.fireWebEvent("onStart", new ActionEvent(url));
}
self.currentURL = url;
} else if ("Loading complete".equals(t1)) {
}
}
});
self.web.getEngine().setOnAlert(new EventHandler<WebEvent<String>>() {
@Override
public void handle(WebEvent<String> t) {
BrowserComponent p = weakP.get();
if (p == null) {
return;
}
String msg = t.getData();
if (msg.startsWith("!cn1_message:")) {
System.out.println("Receiving message " + msg);
p.fireWebEvent("onMessage", new ActionEvent(msg.substring("!cn1_message:".length())));
}
}
});
self.web.getEngine().getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
@Override
public void changed(ObservableValue<? extends Throwable> ov, Throwable t, Throwable t1) {
System.out.println("Received exception: " + t1.getMessage());
if (ov.getValue() != null) {
ov.getValue().printStackTrace();
}
if (t != ov.getValue() && t != null) {
t.printStackTrace();
}
if (t1 != ov.getValue() && t1 != t && t1 != null) {
t.printStackTrace();
}
}
});
self.web.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
@Override
public void changed(ObservableValue ov, State oldState, State newState) {
SEBrowserComponent self = weakSelf.get();
BrowserComponent p = weakP.get();
try {
netscape.javascript.JSObject w = (netscape.javascript.JSObject) self.web.getEngine().executeScript("window");
if (w == null) {
System.err.println("Could not get window");
} else {
Bridge b = new Bridge(p);
self.putClientProperty("SEBrowserComponent.Bridge.jconsole", b);
w.setMember("jconsole", b);
}
} catch (Throwable t) {
Log.e(t);
}
if (self == null || p == null) {
return;
}
String url = self.web.getEngine().getLocation();
if (newState == State.SCHEDULED) {
p.fireWebEvent("onStart", new ActionEvent(url));
} else if (newState == State.RUNNING) {
p.fireWebEvent("onLoadResource", new ActionEvent(url));
} else if (newState == State.SUCCEEDED) {
if (!p.isNativeScrollingEnabled()) {
self.web.getEngine().executeScript("document.body.style.overflow='hidden'");
}
// let's just add a client property to the BrowserComponent to enable firebug
if (Boolean.TRUE.equals(p.getClientProperty("BrowserComponent.firebug"))) {
self.web.getEngine().executeScript("if (!document.getElementById('FirebugLite')){E = document['createElement' + 'NS'] && document.documentElement.namespaceURI;E = E ? document['createElement' + 'NS'](E, 'script') : document['createElement']('script');E['setAttribute']('id', 'FirebugLite');E['setAttribute']('src', 'https://getfirebug.com/' + 'firebug-lite.js' + '#startOpened');E['setAttribute']('FirebugLite', '4');(document['getElementsByTagName']('head')[0] || document['getElementsByTagName']('body')[0]).appendChild(E);E = new Image;E['setAttribute']('src', 'https://getfirebug.com/' + '#startOpened');}");
}
netscape.javascript.JSObject window = (netscape.javascript.JSObject) self.web.getEngine().executeScript("window");
Bridge b = new Bridge(p);
self.putClientProperty("SEBrowserComponent.Bridge.cn1application", b);
window.setMember("cn1application", b);
self.web.getEngine().executeScript("while (window._cn1ready && window._cn1ready.length > 0) {var f = window._cn1ready.shift(); f();}");
// System.out.println("cn1application is "+self.web.getEngine().executeScript("window.cn1application && window.cn1application.shouldNavigate"));
self.web.getEngine().executeScript("window.addEventListener('unload', function(e){console.log('unloading...');return 'foobar';});");
p.fireWebEvent("onLoad", new ActionEvent(url));
}
self.currentURL = url;
self.repaint();
}
});
self.web.getEngine().getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
@Override
public void changed(ObservableValue<? extends Throwable> ov, Throwable t, Throwable t1) {
BrowserComponent p = weakP.get();
if (p == null) {
return;
}
t1.printStackTrace();
if (t1 == null) {
if (t == null) {
p.fireWebEvent("onError", new ActionEvent("Unknown error", -1));
} else {
p.fireWebEvent("onError", new ActionEvent(t.getMessage(), -1));
}
} else {
p.fireWebEvent("onError", new ActionEvent(t1.getMessage(), -1));
}
}
});
// Monitor the location property so that we can send the shouldLoadURL event.
// This allows us to cancel the loading of a URL if we want to handle it ourself.
self.web.getEngine().locationProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> prop, String before, String after) {
SEBrowserComponent self = weakSelf.get();
BrowserComponent p = weakP.get();
if (self == null || p == null) {
return;
}
if (!p.fireBrowserNavigationCallbacks(self.web.getEngine().getLocation())) {
self.web.getEngine().getLoadWorker().cancel();
}
}
});
self.adjustmentListener = new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
Display.getInstance().callSerially(new Runnable() {
public void run() {
SEBrowserComponent self = weakSelf.get();
if (self == null) {
return;
}
self.onPositionSizeChange();
}
});
}
};
}
use of com.codename1.ui.layouts.BorderLayout 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("/iphone3gs.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);
deinitializeSync();
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);
String mainClass = System.getProperty("MainClass");
if (mainClass != null) {
deinitializeSync();
frm.dispose();
System.setProperty("reload.simulator", "true");
}
}
});
skinMenu.addSeparator();
skinMenu.add(dSkin);
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.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);
d.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.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", "/iphone3gs.skin");
deinitializeSync();
frm.dispose();
System.setProperty("reload.simulator", "true");
} else {
loadSkinFile("/iphone3gs.skin", frm);
refreshSkin(frm);
}
}
}
});
return skinMenu;
}
use of com.codename1.ui.layouts.BorderLayout in project CodenameOne by codenameone.
the class Log method showLog.
/**
* Places a form with the log as a TextArea on the screen, this method can
* be attached to appear at a given time or using a fixed global key. Using
* this method might cause a problem with further log output
* @deprecated this method is an outdated method that's no longer supported
*/
public static void showLog() {
try {
String text = getLogContent();
TextArea area = new TextArea(text, 5, 20);
Form f = new Form("Log");
f.setScrollable(false);
final Form current = Display.getInstance().getCurrent();
Command back = new Command("Back") {
public void actionPerformed(ActionEvent ev) {
current.show();
}
};
f.addCommand(back);
f.setBackCommand(back);
f.setLayout(new BorderLayout());
f.addComponent(BorderLayout.CENTER, area);
f.show();
} catch (Exception ex) {
ex.printStackTrace();
}
}
use of com.codename1.ui.layouts.BorderLayout in project CodenameOne by codenameone.
the class Oauth2 method authenticate.
/**
* This method preforms the actual authentication, this method is a blocking
* method that will display the user the html authentication pages.
*
* @return the method if passes authentication will return the access token
* or null if authentication failed.
*
* @throws IOException the method will throw an IOException if something
* went wrong in the communication.
* @deprecated use createAuthComponent or showAuthentication which work
* asynchronously and adapt better to different platforms
*/
public String authenticate() {
if (token == null) {
login = new Dialog();
boolean i = Dialog.isAutoAdjustDialogSize();
Dialog.setAutoAdjustDialogSize(false);
login.setLayout(new BorderLayout());
login.setScrollable(false);
Component html = createLoginComponent(null, null, null, null);
login.addComponent(BorderLayout.CENTER, html);
login.setScrollable(false);
login.setDialogUIID("Container");
login.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, true, 300));
login.setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 300));
login.show(0, 0, 0, 0, false, true);
Dialog.setAutoAdjustDialogSize(i);
}
return token;
}
use of com.codename1.ui.layouts.BorderLayout in project CodenameOne by codenameone.
the class Dialog method initImpl.
private void initImpl(String dialogUIID, String dialogTitleUIID, Layout lm) {
super.getContentPane().setUIID(dialogUIID);
super.getTitleComponent().setText("");
super.getTitleComponent().setVisible(false);
super.getTitleArea().setVisible(false);
super.getTitleArea().setUIID("Container");
lockStyleImages(getUnselectedStyle());
titleArea.setVisible(false);
if (lm != null) {
dialogContentPane = new Container(lm);
} else {
dialogContentPane = new Container();
}
dialogContentPane.setUIID("DialogContentPane");
dialogTitle = new Label("", dialogTitleUIID);
super.getContentPane().setLayout(new BorderLayout());
super.getContentPane().addComponent(BorderLayout.NORTH, dialogTitle);
super.getContentPane().addComponent(BorderLayout.CENTER, dialogContentPane);
super.getContentPane().setScrollable(false);
super.getContentPane().setAlwaysTensile(false);
super.getStyle().setBgTransparency(0);
super.getStyle().setBgImage(null);
super.getStyle().setBorder(null);
setSmoothScrolling(false);
deregisterAnimated(this);
}
Aggregations