/* * EchoSampleServlet.java * */ package xone_sample.echo; import com.fiverworks.xone.model.XoneObject; import java.sql.Timestamp; import nextapp.echo.Button; import nextapp.echo.Color; import nextapp.echo.Component; import nextapp.echo.ContentPane; import nextapp.echo.EchoInstance; import nextapp.echo.Filler; import nextapp.echo.Label; import nextapp.echo.Panel; import nextapp.echo.Table; import nextapp.echo.TextArea; import nextapp.echo.Window; import nextapp.echo.event.ActionEvent; import nextapp.echo.event.ActionListener; import nextapp.echo.table.DefaultTableModel; import nextapp.echo.table.TableCellRenderer; import nextapp.echo.table.TableModel; import nextapp.echoservlet.EchoServer; public class EchoSampleServlet extends EchoServer { public void init() { try { super.init(); XoneFacade.init(); } catch (Exception ex) { ex.printStackTrace(); } } public EchoInstance newInstance() { return new EchoSample(); } public void destroy() { XoneFacade.shutdown(); super.destroy(); } } class EchoSample extends EchoInstance { private XoneObject[] xos; private TextArea textArea; private int selectedRow; private Button saveButton; private Table table; private TableModel createTableModel() { this.xos = XoneFacade.getList(); Object[][] data = new Object[xos.length][5]; for (int i = 0; i < xos.length; i++) { data[i][0] = xos[i].getName(); // オブジェクト名; data[i][1] = xos[i].getElementValue("parent"); // 親フォルダ data[i][2] = xos[i].getElementValue("name"); // ファイル名 data[i][3] = xos[i].getElementValue("lastModified"); // 最終更新日時 data[i][4] = xos[i].getElementValue("length"); // 長さ } Object[] header = (Object[])(new String[]{"オブジェクト名", "親フォルダ", "ファイル名", "最終更新日時", "長さ"}); DefaultTableModel model = new DefaultTableModel(data, header); return model; } private Table createTable() { table = new Table(createTableModel()); table.setCellMargin(3); table.setBorderSize(2); return table; } public Window init() { Window window = new Window(); ContentPane content = new ContentPane(); window.setContent(content); Label label = new Label("オブジェクト一覧"); label.setBackground(Color.GREEN); content.add(label); content.add(Filler.createVerticalStrut(5)); table = this.createTable(); table.setDefaultRenderer(Object.class, new MyRenderer()); content.add(table); content.add(Filler.createVerticalStrut(10)); Label label1 = new Label("内容"); label1.setBackground(Color.GREEN); content.add(label1); content.add(Filler.createVerticalStrut(5)); textArea = new TextArea(50, 20); content.add(textArea); content.add(Filler.createVerticalStrut(5)); saveButton = new Button("上書き保存ボタン"); saveButton.setBackground(Color.MAGENTA); selectedRow = -1; // 選択されていない状態(-1)にセット // クリックされたら選択されているオブジェクトを上書き保存する saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { if (selectedRow == -1) return; XoneObject xo = xos[selectedRow]; // 内容を置き換えて保存 String body = textArea.getText(); xo.setElementValue("body", body); xo.setElementValue("lastModified", new Timestamp(System.currentTimeMillis()).toString()); xo.setElementValue("length", Integer.toString(body.length())); XoneFacade.save(xo); // tableを再初期化 table.setModel(createTableModel()); table.validate(); } }); content.add(saveButton); return window; } // cellのレンダラ用 private class MyRenderer implements TableCellRenderer, ActionListener { public Component getTableCellRendererComponent(Table table, Object value, int col, int row) { if (col == 0) { Button button = new Button((String)value); button.setForeground(Color.WHITE); button.setBackground(Color.BLUE); button.addActionListener(this); return button; } else { Panel cell = new Panel(); if (value != null) { Label label = new Label(value.toString()); cell.add(label); } return cell; } } public void actionPerformed(ActionEvent actionEvent) { Button button = (Button)actionEvent.getSource(); String name = button.getText(); for (int i = 0; i < xos.length; i++) { if (xos[i].getName().equals(name)) { textArea.setText(xos[i].getElementValue("body")); selectedRow = i; break; } } } } }