public void openAndEditFile(String filename) { textIsNew = true; openFile(filename); ctx.getRootContainer().remove(plainText); textEdit.setText(plainText.getText()); ctx.getRootContainer().add(southImage, BorderLayout.SOUTH); ctx.getRootContainer().add(textEdit); textEdit.requestFocus(); }
public void openFile(String filename) { currentFileName = ctx.getHomeDirectory().getAbsolutePath() + "/" + filename + ".txt"; String text = new String(); String str; try { BufferedReader in = new BufferedReader(new FileReader(currentFileName)); try { while ((str = in.readLine()) != null) { text = text + str + "\n"; } } catch (IOException e) { e.printStackTrace(); } try { in.close(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { KOptionPane.showMessageDialog( null, i18n.getString("not_found"), new MessageDialogListener() { public void onClose() { // nothing } }); e.printStackTrace(); return; } this.newItem.setEnabled(false); ctx.getRootContainer().remove(this.homeMenu); // ctx.getRootContainer().remove(this.searchField); ctx.getRootContainer().remove(this.northPanel); ctx.getRootContainer().remove(this.pageLabel); plainText.setText(text); ctx.getRootContainer().add(plainText); plainText.requestFocus(); ctx.setSubTitle(filename); }
public void create(KindletContext context) { avaliableLangs = new ArrayList(); avaliableLangs.add("ru"); avaliableLangs.add("en"); char[] data = context.getSecureStorage().getChars("lang"); if (data != null) currentlang = String.valueOf(data); else currentlang = "ru"; if (!avaliableLangs.contains(currentlang)) currentlang = "ru"; char[] data2 = context.getSecureStorage().getChars("fontSize"); if (data2 != null && data2 != data) fontSize = Integer.parseInt(String.valueOf(data2)); else fontSize = 21; i18n = ResourceBundle.getBundle("lang/lang", new Locale(currentlang)); this.itemsList = new ArrayList(); this.northPanel = new KPanel(new BorderLayout()); this.northPanel.add(new KLabel(i18n.getString("filter")), BorderLayout.WEST); this.ctx = context; this.pageLabel = new KLabel("KindleNote by proDOOMman", KLabel.CENTER); this.pageLabel.setForeground(Color.white); this.pageLabel.setBackground(Color.black); ctx.getRootContainer().add(pageLabel, BorderLayout.SOUTH); this.menu = new KMenu(); this.searchField = new KTextField(); this.northPanel.add(this.searchField); this.searchField.setBorder(new KLineBorder(3, true)); this.searchField.addTextListener( new TextListener() { public void textValueChanged(TextEvent arg0) { String tofind = searchField.getText(); ListIterator iterator = itemsList.listIterator(itemsList.size()); homeMenu.removeAllItems(); List tempItemsList = new ArrayList(); while (iterator.hasPrevious()) { KFakeMenuItem element = (KFakeMenuItem) iterator.previous(); String text = element.getText(); boolean found = true; if (text.indexOf(tofind) == -1) found = false; if (found) tempItemsList.add(element); } Object[] tempItems = tempItemsList.toArray(); Arrays.sort( tempItems, new Comparator() { public int compare(Object arg0, Object arg1) { KFakeMenuItem o1 = (KFakeMenuItem) arg0; File f1 = new File(ctx.getHomeDirectory() + "/" + o1.getText() + ".txt"); KFakeMenuItem o2 = (KFakeMenuItem) arg1; File f2 = new File(ctx.getHomeDirectory() + "/" + o2.getText() + ".txt"); return f1.lastModified() > f2.lastModified() ? -1 : f1.lastModified() == f2.lastModified() ? 0 : 1; } }); for (int i = 0; i < tempItems.length; i++) homeMenu.addItem((KFakeMenuItem) tempItems[i]); homeMenu.repaint(); } }); // ctx.getRootContainer().add(searchField, BorderLayout.NORTH); ctx.getRootContainer().add(northPanel, BorderLayout.NORTH); this.newItem = new KMenuItem(i18n.getString("new_note")); this.newItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { newItem(); } }); this.menu.add(newItem); KMenuItem tempItem = new KMenuItem(i18n.getString("control")); tempItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { KOptionPane.showMessageDialog( ctx.getRootContainer(), i18n.getString("control_text"), new MessageDialogListener() { public void onClose() { // nothing } }); } catch (Exception e) { e.printStackTrace(); } } }); this.menu.add(tempItem); tempItem = new KMenuItem(i18n.getString("help")); tempItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { KOptionPane.showMessageDialog( ctx.getRootContainer(), i18n.getString("help_text"), new MessageDialogListener() { public void onClose() { // nothing } }); } catch (Exception e) { e.printStackTrace(); } } }); this.menu.add(tempItem); tempItem = new KMenuItem(i18n.getString("about")); tempItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { KOptionPane.showMessageDialog( ctx.getRootContainer(), i18n.getString("about_text"), new MessageDialogListener() { public void onClose() { // nothing } }); } catch (Exception e) { e.printStackTrace(); } } }); this.menu.add(tempItem); this.ctx.setMenu(this.menu); this.textEdit = new KTextArea() { private static final long serialVersionUID = 745146258399462745L; protected void processFocusEvent(FocusEvent e) { FocusListener[] listeners = getFocusListeners(); for (int j = 0; j < listeners.length; j++) { int id = e.getID(); switch (id) { case FocusEvent.FOCUS_GAINED: listeners[j].focusGained(e); break; case FocusEvent.FOCUS_LOST: listeners[j].focusLost(e); break; } } } }; this.textEdit.addKeyListener( new KeyListener() { public void keyTyped(KeyEvent e) { if ((e.getKeyChar() == 'D' || e.getKeyChar() == 'd') && ((e.getModifiersEx() & KeyEvent.ALT_DOWN_MASK) != 0)) { Date dtn = new Date(); SimpleDateFormat formatter1 = new SimpleDateFormat("dd.MM.yyyy HH:mm"); String dt = formatter1.format(dtn); String s = textEdit.getText(); int pos = textEdit.getCaretPosition(); textEdit.setText(s.substring(0, pos) + dt + s.substring(pos, s.length())); textEdit.setCaretPosition(pos + dt.length()); textEdit.repaint(); } } public void keyReleased(KeyEvent e) {} public void keyPressed(KeyEvent e) {} }); this.textEdit.setFont( new Font(textEdit.getFont().getName(), textEdit.getFont().getStyle(), fontSize)); this.textEdit.addKeyListener( new KeyListener() { public void keyTyped(KeyEvent arg0) { if (arg0.getKeyCode() == KindleKeyCodes.VK_BACK) { arg0.consume(); } } public void keyReleased(KeyEvent arg0) { if (arg0.getKeyCode() == KindleKeyCodes.VK_BACK) { if (textIsNew) { try { FileWriter outFile = new FileWriter(currentFileName); PrintWriter out = new PrintWriter(outFile); out.print(textEdit.getText()); out.close(); } catch (IOException e) { e.printStackTrace(); } plainText.setText(textEdit.getText()); homeMenu.removeItem(lastFocus); homeMenu.addItem(lastFocus, 0); textIsNew = false; } else if (plainText.getText().compareTo(textEdit.getText()) != 0) { KOptionPane.showConfirmDialog( ctx.getRootContainer(), i18n.getString("save"), new ConfirmDialogListener() { public void onClose(int arg0) { if (arg0 == KOptionPane.OK_OPTION) { try { FileWriter outFile = new FileWriter(currentFileName); PrintWriter out = new PrintWriter(outFile); out.print(textEdit.getText()); out.close(); } catch (IOException e) { e.printStackTrace(); } plainText.setText(textEdit.getText()); homeMenu.removeItem(lastFocus); homeMenu.addItem(lastFocus, 0); } } }); } ctx.getRootContainer().remove(textEdit); if (southImage != null) ctx.getRootContainer().remove(southImage); ctx.getRootContainer().add(plainText); plainText.requestFocus(); arg0.consume(); } else if (arg0.getKeyCode() == KindleKeyCodes.VK_FIVE_WAY_SELECT) { // выделение текста } } public void keyPressed(KeyEvent arg0) { if (arg0.getKeyCode() == KindleKeyCodes.VK_BACK) { arg0.consume(); } } }); this.plainText = new KLabelMultiline(); this.plainText.setFont( new Font(plainText.getFont().getName(), plainText.getFont().getStyle(), fontSize)); this.plainText.setFocusable(true); this.plainText.addKeyListener( new KeyListener() { public void keyTyped(KeyEvent arg0) { if (arg0.getKeyCode() == KindleKeyCodes.VK_BACK) { arg0.consume(); } } public void keyReleased(KeyEvent arg0) { if (arg0.getKeyCode() == KindleKeyCodes.VK_BACK) { ctx.getRootContainer().remove(plainText); ctx.getRootContainer().add(homeMenu); // ctx.getRootContainer().add(searchField, BorderLayout.NORTH); ctx.getRootContainer().add(northPanel, BorderLayout.NORTH); ctx.getRootContainer().add(pageLabel, BorderLayout.SOUTH); ctx.setSubTitle(""); currentFileName = ""; newItem.setEnabled(true); homeMenu.requestFocus(); lastFocus.requestFocus(); arg0.consume(); } else // if(arg0.getKeyCode() == KindleKeyCodes.VK_FIVE_WAY_SELECT) { ctx.getRootContainer().remove(plainText); textEdit.setText(plainText.getText()); ctx.getRootContainer().add(textEdit); if (southImage != null) ctx.getRootContainer().add(southImage, BorderLayout.SOUTH); textEdit.requestFocus(); } } public void keyPressed(KeyEvent arg0) { if (arg0.getKeyCode() == KindleKeyCodes.VK_BACK) { arg0.consume(); } } }); }