Java tutorial
/* * Copyright (c) 2014. WillowTree Apps * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.willowtreeapps.respeeker.idea; import com.android.ddmlib.IDevice; import com.willowtreeapps.respeeker.idea.res.ConfigInfo; import org.apache.commons.lang.StringUtils; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** * User: evantatarka * Date: 3/24/14 * Time: 9:50 AM */ public class ResPeekerTools { private JCheckBox navigateOnTouchCheckbox; private JPanel toolWindow; private JButton navigateToLayoutButton; private JLabel apiLevelLabel; private JLabel orientationLabel; private JLabel sizeLabel; private JLabel densityLabel; private JLabel localLabel; private JLabel dimensionLabel; private JComboBox<DeviceItem> deviceCombo; private DefaultComboBoxModel<DeviceItem> devicesModel; private OnNavigate onNavigateListener; public ResPeekerTools() { navigateToLayoutButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { navigate(); } }); deviceCombo.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { DeviceItem deviceItem = getSelectedDeviceItem(); updateLayoutInfo(deviceItem == null ? null : deviceItem.layoutInfo); updateConfigInfo(deviceItem == null ? null : deviceItem.configInfo); } }); } public JPanel getPanel() { return toolWindow; } public boolean alwaysNavigate() { return navigateOnTouchCheckbox.isSelected(); } public void setDevices(IDevice[] devices) { devicesModel = new DefaultComboBoxModel<DeviceItem>(); for (IDevice device : devices) devicesModel.addElement(new DeviceItem(device)); deviceCombo.setModel(devicesModel); } public void addDevice(IDevice device) { DeviceItem deviceItem = new DeviceItem(device); devicesModel.addElement(deviceItem); devicesModel.setSelectedItem(deviceItem); } public void removeDevice(IDevice device) { DeviceItem deviceItem = findDeviceItem(device); if (deviceItem != null) { devicesModel.removeElement(deviceItem); } } public void setConfigInfo(IDevice device, ConfigInfo configInfo) { findDeviceItem(device).configInfo = configInfo; if (getSelectedDeviceItem().device.equals(device)) { updateConfigInfo(configInfo); } } public void setLayoutInfo(IDevice device, LayoutInfo layoutInfo) { DeviceItem deviceItem = findDeviceItem(device); deviceItem.layoutInfo = layoutInfo; devicesModel.setSelectedItem(deviceItem); updateLayoutInfo(layoutInfo); if (alwaysNavigate()) navigate(); } private DeviceItem getSelectedDeviceItem() { return (DeviceItem) devicesModel.getSelectedItem(); } private void updateConfigInfo(ConfigInfo configInfo) { apiLevelLabel.setText(configInfo == null ? "" : configInfo.getApiLevel()); orientationLabel.setText(configInfo == null ? "" : configInfo.getOrientation()); sizeLabel.setText(configInfo == null ? "" : configInfo.getSize()); densityLabel.setText(configInfo == null ? "" : configInfo.getDensity()); localLabel.setText(configInfo == null ? "" : configInfo.getLocale()); dimensionLabel.setText(configInfo == null ? "" : configInfo.getWidthDp() + "x" + configInfo.getHeightDp()); } private void updateLayoutInfo(LayoutInfo layoutInfo) { navigateToLayoutButton.setText(layoutInfo == null ? "unknown" : layoutInfo.getLayoutName()); } private DeviceItem findDeviceItem(IDevice device) { for (int i = 0; i < devicesModel.getSize(); i++) { DeviceItem deviceItem = devicesModel.getElementAt(i); if (deviceItem.device.equals(device)) return deviceItem; } return null; } private void navigate() { DeviceItem deviceItem = getSelectedDeviceItem(); if (onNavigateListener != null && deviceItem != null && deviceItem.configInfo != null && deviceItem.layoutInfo != null) { onNavigateListener.onNavigate(deviceItem.layoutInfo, deviceItem.configInfo); } } public void setOnNavigateListener(OnNavigate listener) { onNavigateListener = listener; } public interface OnNavigate { void onNavigate(LayoutInfo layoutInfo, ConfigInfo configInfo); } private static class DeviceItem { public final IDevice device; public LayoutInfo layoutInfo; public ConfigInfo configInfo; private String name; public DeviceItem(IDevice device) { this.device = device; } @Override public String toString() { return getName(); } public String getName() { if (name == null) { String manufacturer = StringUtils.capitalize(device.getProperty("ro.product.manufacturer")); String model = device.getProperty("ro.product.model"); if (manufacturer != null && model != null) { name = manufacturer + " " + model; } } return name; } } }