Posts

Python: Scope

Global variables are confusing and the use should be avoided. If the variable is defined elsewhere, tracing the use of the variables gets hard and gets even harder for larger projects. The variable scope of Python is not as 'strict' as in C. The variables within for-loop are not shielded. If assigned a value outside the loop, the variable within the loop will be affected. a = 0 for i in range( 0 , 1 ) : a = 1 print (a) On the other hand, the variables within a function are shielded. If the variables should be shared outside the scope of the function, global keyword specifies that the variable values are shared. def func (): global a a = 0 a = 1 func() print (a)

Python: Late Binding

Late binding, aka late evaluation is, good; it saves the machine time. Do not evaluate unless you need it. What then, if a system evaluate the file before no one is ever sure that the file would be used or not? Here is something that you can have a very strong opinion on. Would you want the system evaluating just by open()ing the file? Look at the following code in Python. This is just opening up a log file. import logging LOGFORMAT = '[ %(asctime)s ] %(filename)s ( %(lineno)d ): %(message)s ' logging.basicConfig( filename = 'log/books.log' , format = LOGFORMAT, datefmt = ' %Y - %m - %d %H : %M : %S ' , level = logging.DEBUG) I am just setting the filename for logging. The file name could just be anything. <code>dummy.txt</code>, <code>&gt; null</code> thing. Right? Then it took me a while decoding the error message that the Python is giving me. "Server error! The server encountered an inte

Spring Tool Suite

The Spring Framework offers a safe webapp framework with integrated Hibernate style database mapping. With Spring Tool Suite, all the config parameter settings are taken care of. Using STS, you can start out writing the Spring app rıght away. A word of caution, it may demand JVM (32bit version). → Spring Tool Suite The default port number 8080. That can be set by specifying it in src/main/resources/application.properties file. server . port = 8090 Using STS, the database mapping called JPA is as simple as specifying in pom.xml. The mapped class should be specified by annotation. As for HTML pages, Thyme leaf is recommended and instead of JSP. < dependencies > < dependency > < groupId > org.springframework.boot </ groupId > < artifactId > spring-boot-starter-data-jpa </ artifactId > </ dependency > < dependency > < groupId > org.springframework.b

TreeEditor: Customizing Drag and Drop

JTree is a Java GUI component for tree structure.  The tree structure is represented by nodes ( DefaultMutableTreeNode ).  The path or the placement of the node in the tree is represented by TreePath . DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); Use TransferHandler for drag and drop.  Supposing a class derived from TransferHandler called MenuTransferHandler handles drag and drop, set the TransferHandler as follows: MenuTransferHandler handler = new MenuTransferHandler(); treeEditor . tree .setTransferHandler(handler); The copied new node should be generated at TransferHandler#createTransferable() .  Here shows the function copy() that may come in handy in writing createTransferable() .   copy() should be called recursively. private DefaultMutableTreeNode copy (TreeNode node) { DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(node); if (!node.isLeaf()) { @Supp

JPopupMenu

Setting up a right click menu, or popup menu in Java is a tricky but simple procedure.  You would instantiate JPopupMenu, set JMenuItem, attach an ActionListener, set MouseListener.  The beauty of Java GUI structure is in that the listener can be set anywhere.  Any arbitrary component can implement the listener.  A JFrame can have a JPopupMenu and any component can set the MouseListener that set the popup.  And then any component can process the selected JMenuItem. Supposing say a JFrame has a JTable and a JTextArea.  You would want different JPopupMenu.  Each component can possess the JMenuPopup of its own, and the menu can invoke the function in JFrame and not only of JTable nor JTextArea. Which component should have the JPopupMenu (usually the sub-component but can be anywhere) determines what should implements the MouseListener.  The JMenuItem in the JPopupMenu should be 'listened' by an ActionListener that should process the demand. Here is a project that uses a JPop

Quasi Abstraction of the JMenu system

Image
Setting up the menu for JFrame is a matter of creating JMenuBar, JMenu, JMenuItem, add()-ing the components and setting ActionListener for JMenuItem.  The each menu item must be associated with the menu, and the menu must be associated with the menubar.  The ActionListener must be set for each menu item, and the ActionEvent must be processed appropriately. This is a process that can be automated.  It would be far more easier if the menu item can be processed in some managed way.  There are ways of thinking on this: for smaller system, it would be more intuitive if the action is defined in the proximity of the menu item being declared.  In a larger system, it gets unmanageable unless all the accesses are not processed in a centralized manner. →  JavaMenuMenu.java Here I show a way that this procedure can be if not abstracted then greatly simplified by creating a menu class.  Here each menu items is associated with a tag.  A hashtable as the lookup table for the tag and the menu i

TreeEditor: editable tree GUI component

Image
TreeEditor is a Java GUI component for editing tree structure.  It is a JScrollPane (as of TreeEditor ver. 0.1.0) derived and you can directly embed the component in your app.  I would suggest using JSplitPane . First and foremost, load the TreeEditor-0.1.5 library (if you are using Maven) as follows. < dependency > < groupId > com.github.easai.utils </ groupId > < artifactId > TreeEditor </ artifactId > < version > 0.1.6 </ version > </ dependency > Instantiate TreeEditor with a file name (or an empty string).    Initialize it by calling TreeEditor#init() .  Here I put it in a JSplitPane  and the TreeEditor will be resizable as the pane resizes. TreeEditor treeEditor = new TreeEditor( "" ); JTextArea menuCode = new JTextArea(); JTextArea listenerCode = new JTextArea(); public void init () { treeEditor. init (); JSplitPan