Presentation is loading. Please wait.

Presentation is loading. Please wait.

עקרונות תכנות מונחה עצמים תרגול 8: MVC. Outline  MVC  Using the default models  Example- File Browser.

Similar presentations


Presentation on theme: "עקרונות תכנות מונחה עצמים תרגול 8: MVC. Outline  MVC  Using the default models  Example- File Browser."— Presentation transcript:

1 עקרונות תכנות מונחה עצמים תרגול 8: MVC

2 Outline  MVC  Using the default models  Example- File Browser

3 MVC (Model View Controller) “Model–view–controller ( MVC ) is a software architectural pattern for implementing user interfaces. It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user.” -Wikipedia

4  A model stores data that is retrieved by the controller and displayed in the view. Whenever there is a change to the data it is updated by the controller MVC - Model

5 MVC - View  A view requests information from the model that it uses to generate an output representation to the user

6 MVC - Controller  A controller can send commands to the model to update the model's state (e.g., editing a document). It can also send commands to its associated view to change the view's presentation of the model (e.g., by scrolling through a document)

7 Outline  MVC  Using the default models  Example- File Browser

8  Swing supplies default, mutable models for all widgets:  List  Tree (nodes)  Table  Spinner  Slider  … Using the Default Models

9 getModel public boolean isPressed() { return getModel().isPressed(); } JButton +isPressed: boolean +getModel: ButtonModel ButtonModel +isPressed: boolean

10 Example 1 DefaultListModel

11 Default List Model Example Add and remove elements from a list using the default model: public class SimpleList extends JFrame { private JList _list; private DefaultListModel _model;... }

12 Constructor with Default List Model public SimpleList() { super( "ListDemo" ); setModel(new DefaultListModel()); setList( new JList(_model)); getList().setPreferredSize( new Dimension(100, 300)); getContentPane().add(getList(), BorderLayout.CENTER); JToolBar tBar = new JToolBar(); JButton tAdd = new JButton("+"); // calls add() tBar.add(tAdd); JButton tRem = new JButton("-"); // calls rem() tBar.add(tRem); getContentPane().add(tBar, BorderLayout.NORTH);... }

13 Adding and Removing with the Default Model private void add() { String tVal = JOptionPane.showInputDialog( this, "Add new value"); if (tVal != null) { getModel().add(getModel().getSize(), tVal); } private void rem() { int[] is = getList().getSelectedIndices(); Arrays.sort(is); for (int i = is.length - 1; i >= 0; i--) { getModel().remove(is[i]); }}

14 Outline  MVC  Using the default models  Example- File Browser

15  The file browser shows:  Directories tree  List of files in the selected directory  Content of the selected file  Plus:  The event dispatcher thread should not hang while reading the content of a file  Only the event dispatcher thread should update the screen A Simple File Broswer

16 Example 2 FileBrowser

17  Tree Model  Tree Cell Render  List Cell Render  The File Browser  Writing a swing worker Model View Controller

18 File Browser  Tree Model  Tree Cell Render  List Cell Render  The File Browser  Writing a swing worker

19  Tree model  Get Root  Get Children count per node  Get Childe for index  Check if a node is leaf  Directories tree model  Root = Top directory  Children = sub-directories in the directory  Leaf = A directory without sub-directory A Model for the Tree View

20 The Directories Tree Model public class DirTreeModel implements TreeModel { private File _dir; public DirTreeModel(File dir) { setDir(dir); } private static FileFilter _FILTER = new FileFilter() { public boolean accept(File file) { return file.isDirectory(); } }; private File[] list(File file) { return file.listFiles(_FILTER); } Inner class

21 Tree Model Inferface Methods public Object getRoot() { return getDir(); } public Object getChild(Object parent, int index) { File[] tChildren = list((File)parent); return tChildren[index]; } public int getChildCount(Object parent) { return list((File)parent).length; }

22 Tree Model Interface Methods (cont’d) public boolean isLeaf(Object node) { return getChildCount(node) == 0; } public int getIndexOfChild(Object parent, Object child) { List tFiles = Arrays.asList(list((File)parent)); return tFiles.indexOf((File)child); }

23 File Browser  Tree Model  Tree Cell Render  List Cell Render  The File Browser  Writing a swing worker

24 public class DirTreeCellRender extends DefaultTreeCellRender{ public Component GetTreeCellRenderComponent( JTree tree, Object Value, boolean cell, boolean expended, boolean leaf, int row, boolean hasFocus) { super.getCellRenderComponent(…); File tRoot = (File) getModel().getRoot(); File tFile = (File)value; setText(tRoot.equals(value)?tFile.getAbsolutePath(): tFile.getName()); setIcon( expanded? openIcon : closeIcon ); return this; }} Rendering a Directories Tree Cell

25 Files List Model public class FilesListModel implements ListModel { private File _dir; private List _files; private Collection _listeners; private static FileFilter _FILTER = new FileFilter() { public boolean accept(File f) { return f.isFile(); } }; public FilesListModel() { setListeners(new LinkedList ()); setFiles(Collections.EMPTY_LIST); } 18 / 31

26 Setting the Directory public File getDir() { return _dir; } public void setDir(File dir) { _dir = dir; int tOldSize = getSize(); File[] tFiles = dir.listFiles(_FILTER); setFiles(Arrays.asList(tFiles)); int tMax = Math.max(tOldSize, getSize()); ListDataEvent tEvt = new ListDataEvent(this,LDE.CONTENTS_CHANGED, 0,tMax); for (ListDataListener tListener : getListeners()) { tListener.contentsChanged(tEvt); { 19 / 31

27 Files List Model Interface Methods public int getSize() { return getFiles().size(); } public Object getElementAt( int index) { return getFiles().get(index); } public void addListDataListener(ListDataListener l) { getListeners().add(l); } public void removeListDataListener(ListDataListener l) { getListeners().remove(l); }

28 File Browser  Tree Model  Tree Cell Render  List Cell Render  The File Browser  Writing a swing worker

29 Rendering a Files List Cell public class FilesListCellRenderer extends DefaultListCellRenderer{ private static Color _ALT = new Color(220, 220, 220); public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus( { super.getListCellRendererComponent(...); File f = (File) value; setText(f.getName()); if (!isSelected && index % 2 == 0){ setBackground(_ALT); { return this; } {

30 File Browser  Tree Model  Tree Cell Render  List Cell Render  The File Browser  Writing a swing worker

31 Putting It Together public class FileBrowser extends JFrame implements TreeSelectionListener, ListSelectionListener { private JTree _tree; private JTextArea _content; private JList _files;... }

32 File Broswer Constructor public FileBrowser(File file) { super(file.getAbsolutePath()); // Tree setTree(new JTree(new DirTreeModel(file))); getTree().setCellRenderer(new DirTreeCellRenderer()); getTree().addTreeSelectionListener(this); // File list setFiles(new JList(new FilesListModel())); getFiles().setCellRenderer(new FilesListCellRenderer()); getFiles().setSelectionMode(LSM.SINGLE_SELECTION); getFiles().addListSelectionListener(this); // Text area setContent(new JTextArea(10, 30));... }

33 Listening to Tree Events public void valueChanged(TreeSelectionEvent e) { File tDir = (File) e.getPath().getLastPathComponent(); ((FilesListModel)getFiles().getModel()).setDir(tDir); }

34 Listening to List Events public void valueChanged(ListSelectionEvent e) { // Gotcha 1: Check if value is adjusting if (e.getValueIsAdjusting()) return; // Gotcha 2: Don’t use e’s indices File tFile = (File) getFiles().getSelectedValue(); getContent().setText(""); // Gotcha 3: Don’t re-use a worker SwingWorker tWorker = new FileReaderWorker(tFile, getContent()); tWorker.execute(); }

35 File Browser  Tree Model  Tree Cell Render  List Cell Render  The File Browser  Writing a swing worker

36 Swing worker  Abstract Class: SwingWorker  protected abstract T doInBackground()  protected void process(List chunks)  protected void done()  protected final void publish(V... chunks)

37 Writing a Swing Worker public class FileReaderWorker extends SwingWorker { private File _file; private JTextArea _text;... }

38 Do in Background protected Void doInBackground() throws Exception { try { BufferedReader tIn = new BufferedReader(new FileReader(getFile())); String tLine; while ((tLine = tIn.readLine()) != null) { publish(tLine); } tIn.close(); } catch (IOException e) { publish(e.getMessage()); } return null; }

39 Process and Done protected void process(List chunks) { for (String tLine : chunks) { getText().append(tLine + "\n"); } protected void done() { getText().setCaretPosition(0); }


Download ppt "עקרונות תכנות מונחה עצמים תרגול 8: MVC. Outline  MVC  Using the default models  Example- File Browser."

Similar presentations


Ads by Google