use of com.codename1.ui.List in project CodenameOne by codenameone.
the class DefaultLookAndFeel method getListPreferredSizeImpl.
private Dimension getListPreferredSizeImpl(List l) {
int width = 0;
int height = 0;
int selectedHeight;
int selectedWidth;
ListModel model = l.getModel();
int numOfcomponents = Math.max(model.getSize(), l.getMinElementHeight());
numOfcomponents = Math.min(numOfcomponents, l.getMaxElementHeight());
Object prototype = l.getRenderingPrototype();
Style unselectedEntryStyle = null;
Style selectedEntryStyle;
if (prototype != null) {
ListCellRenderer renderer = l.getRenderer();
Component cmp = renderer.getListCellRendererComponent(l, prototype, 0, false);
height = cmp.getPreferredH();
width = cmp.getPreferredW();
unselectedEntryStyle = cmp.getStyle();
cmp = renderer.getListCellRendererComponent(l, prototype, 0, true);
selectedEntryStyle = cmp.getStyle();
selectedHeight = Math.max(height, cmp.getPreferredH());
selectedWidth = Math.max(width, cmp.getPreferredW());
} else {
int hightCalcComponents = Math.min(l.getListSizeCalculationSampleCount(), numOfcomponents);
Object dummyProto = l.getRenderingPrototype();
if (model.getSize() > 0 && dummyProto == null) {
dummyProto = model.getItemAt(0);
}
ListCellRenderer renderer = l.getRenderer();
for (int i = 0; i < hightCalcComponents; i++) {
Object value;
if (i < model.getSize()) {
value = model.getItemAt(i);
} else {
value = dummyProto;
}
Component cmp = renderer.getListCellRendererComponent(l, value, i, false);
if (cmp instanceof Container) {
cmp.setShouldCalcPreferredSize(true);
}
unselectedEntryStyle = cmp.getStyle();
height = Math.max(height, cmp.getPreferredH());
width = Math.max(width, cmp.getPreferredW());
}
selectedEntryStyle = unselectedEntryStyle;
selectedHeight = height;
selectedWidth = width;
if (model.getSize() > 0) {
Object value = model.getItemAt(0);
Component cmp = renderer.getListCellRendererComponent(l, value, 0, true);
if (cmp instanceof Container) {
cmp.setShouldCalcPreferredSize(true);
}
selectedHeight = Math.max(height, cmp.getPreferredH());
selectedWidth = Math.max(width, cmp.getPreferredW());
selectedEntryStyle = cmp.getStyle();
}
}
if (unselectedEntryStyle != null) {
selectedWidth += selectedEntryStyle.getMarginLeftNoRTL() + selectedEntryStyle.getMarginRightNoRTL();
selectedHeight += selectedEntryStyle.getMarginTop() + selectedEntryStyle.getMarginBottom();
width += unselectedEntryStyle.getMarginLeftNoRTL() + unselectedEntryStyle.getMarginRightNoRTL();
height += unselectedEntryStyle.getMarginTop() + unselectedEntryStyle.getMarginBottom();
}
Style lStyle = l.getStyle();
int verticalPadding = lStyle.getPaddingTop() + lStyle.getPaddingBottom();
int horizontalPadding = lStyle.getPaddingRightNoRTL() + lStyle.getPaddingLeftNoRTL() + l.getSideGap();
if (numOfcomponents == 0) {
return new Dimension(horizontalPadding, verticalPadding);
}
// If combobox without ever importing the ComboBox class dependency
if (l.getOrientation() > List.HORIZONTAL) {
int boxWidth = l.getStyle().getFont().getHeight() + 2;
return new Dimension(boxWidth + selectedWidth + horizontalPadding, selectedHeight + verticalPadding);
} else {
if (l.getOrientation() == List.VERTICAL) {
return new Dimension(selectedWidth + horizontalPadding, selectedHeight + (height + l.getItemGap()) * (numOfcomponents - 1) + verticalPadding);
} else {
return new Dimension(selectedWidth + (width + l.getItemGap()) * (numOfcomponents - 1) + horizontalPadding, selectedHeight + verticalPadding);
}
}
}
use of com.codename1.ui.List in project CodenameOne by codenameone.
the class UserInterfaceEditor method reflowTableLayout.
private void reflowTableLayout(com.codename1.ui.Container parent) {
// affects the layout initially
if (parent.getLayout() instanceof com.codename1.ui.table.TableLayout) {
List<com.codename1.ui.Component> cmpList = new ArrayList<com.codename1.ui.Component>();
List constraintList = new ArrayList();
for (int iter = 0; iter < parent.getComponentCount(); iter++) {
com.codename1.ui.Component currentCmp = parent.getComponentAt(iter);
cmpList.add(currentCmp);
constraintList.add(parent.getLayout().getComponentConstraint(currentCmp));
}
parent.removeAll();
for (int iter = 0; iter < cmpList.size(); iter++) {
parent.addComponent(constraintList.get(iter), cmpList.get(iter));
}
}
}
use of com.codename1.ui.List in project CodenameOne by codenameone.
the class UserInterfaceEditor method getListOfAllCommands.
private static List<com.codename1.ui.Command> getListOfAllCommands(EditableResources res) {
final List<com.codename1.ui.Command> response = new ArrayList<com.codename1.ui.Command>();
UIBuilderOverride tempBuilder = new UIBuilderOverride() {
public com.codename1.ui.Command createCommandImpl(String commandName, com.codename1.ui.Image icon, int commandId, String action, boolean isBack, String arg) {
com.codename1.ui.Command c = super.createCommandImpl(commandName, icon, commandId, action, isBack, arg);
if (!response.contains(c)) {
response.add(c);
}
return c;
}
};
for (String uiResourceName : res.getUIResourceNames()) {
tempBuilder.createContainer(res, uiResourceName);
}
Collections.sort(response, new Comparator<com.codename1.ui.Command>() {
public int compare(com.codename1.ui.Command o1, com.codename1.ui.Command o2) {
if (o1 == null) {
if (o2 == null) {
return 0;
}
return -1;
}
if (o2 == null) {
return 1;
}
return String.CASE_INSENSITIVE_ORDER.compare(o1.getCommandName(), o2.getCommandName());
}
});
return response;
}
use of com.codename1.ui.List in project CodenameOne by codenameone.
the class EditableResources method saveL10N.
private void saveL10N(DataOutputStream output, Hashtable l10n) throws IOException {
List keys = new ArrayList();
for (Object locale : l10n.keySet()) {
Hashtable current = (Hashtable) l10n.get(locale);
for (Object key : current.keySet()) {
if (!keys.contains(key)) {
keys.add(key);
}
}
}
output.writeShort(keys.size());
output.writeShort(l10n.size());
for (Object key : keys) {
output.writeUTF((String) key);
}
for (Object locale : l10n.keySet()) {
Hashtable currentLanguage = (Hashtable) l10n.get(locale);
output.writeUTF((String) locale);
for (Object key : keys) {
String k = (String) currentLanguage.get(key);
if (k != null) {
output.writeUTF(k);
} else {
output.writeUTF("");
}
}
}
}
use of com.codename1.ui.List in project CodenameOne by codenameone.
the class JavaSEPort method openInputStream.
/**
* @inheritDoc
*/
public InputStream openInputStream(Object connection) throws IOException {
if (connection instanceof String) {
FileInputStream fc = new FileInputStream(unfile((String) connection));
BufferedInputStream o = new BufferedInputStream(fc, (String) connection);
return o;
}
if (netMonitor != null || slowConnectionMode || disconnectedMode) {
final NetworkRequestObject nr = getByConnection((URLConnection) connection);
if (nr != null || slowConnectionMode || disconnectedMode) {
if (slowConnectionMode) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
if (disconnectedMode) {
throw new IOException("Unreachable");
}
HttpURLConnection con = (HttpURLConnection) connection;
String headers = "";
Map<String, List<String>> map = con.getHeaderFields();
for (String header : map.keySet()) {
headers += header + "=" + map.get(header) + "\n";
}
if (nr != null) {
nr.setResponseHeaders(headers);
nr.setResponseBody("");
}
InputStream is;
if (con.getResponseCode() >= 200 && con.getResponseCode() < 300) {
is = con.getInputStream();
} else {
is = con.getErrorStream();
}
InputStream i = new BufferedInputStream(is) {
public synchronized int read(byte[] b, int off, int len) throws IOException {
int s = super.read(b, off, len);
if (nr != null) {
if (s > -1) {
nr.setResponseBody(nr.getResponseBody() + new String(b, off, len));
}
}
if (slowConnectionMode) {
try {
Thread.sleep(len);
} catch (Exception e) {
}
}
if (disconnectedMode) {
throw new IOException("Unreachable");
}
return s;
}
};
return i;
}
}
if (connection instanceof HttpURLConnection) {
HttpURLConnection ht = (HttpURLConnection) connection;
if (ht.getResponseCode() < 400) {
return new BufferedInputStream(ht.getInputStream());
}
return new BufferedInputStream(ht.getErrorStream());
} else {
return new BufferedInputStream(((URLConnection) connection).getInputStream());
}
}
Aggregations