Java : Reading properties/settings from a file
Created 14 May 2002 00:00
You can load properties stored in a key=value format (each property on a new line) using an InputStream and a Properties object:
  BufferedInputStream bis = null;
  Properties prop = null;
  String strFile = "package" + File.separator + "properties.file";
  // load properties file
  try {
    bis = new BufferedInputStream(new FileInputStream(new File(strFile)));  
  }
  catch (FileNotFoundException fnfe) {
    // throw error here
  }
  // load properties variables with details in the file
  prop = new Properties();
  try {
    prop.load(bis);
    // load all the properties
    strProperty = prop.getProperty("keyname");	
    // close the input stream
    bis.close();
  }
  catch (Exception ioe) {
    // throw error
  }
Java Undo / Redo functionality
Created 14 May 2002 00:00, updated 17 June 2017 10:00
Undo / Redo functionality can be added to document classes in the javax.swing.text package very easily. All you need to do is add a listener for changes, and use a UndoManager to alter those changes.

For example, you have a JTextPane which is using a DefaultStyledDocument as it's document source:

public class MyTextPane extends JTextPane {
  
  // create undo manager
  private UndoManager undo = new UndoManager();
  
  public MyTextPane() {
    ...
    // add undoable edit listener to the document
    this.getStyledDocument().addUndoableEditListener(new MyUndoableEditListener());
  }

  // add undo method
  public void undo() {
    undo.undo();
  }

  // add redo method
  public void redo() {
    undo.redo();
  }

  // create private class for listening to document changes
  private class StdUndoableEditListener implements UndoableEditListener {
    public void undoableEditHappened(UndoableEditEvent e) {
      // add each edit to the undo manager
      undo.addEdit(e.getEdit());
    }
  }
}
Java Drag and Drop
Created 16 May 2002 00:00
If, for example you want an application to support the dragging of files from a file list to your component, and your component to open those files, use the following code:

//import drag and drop and datatransfer packages
import java.awt.dnd.*;
import java.awt.datatransfer.*;

public class MyClass extends AnotherClass {

  // declare variables - including a DropTarget object to which you will 
  // link a target component
  private DropTarget dropTarget = null;

  public MyClass() {
    // in constructor or initialisation method, initialise the DropTarget 
    // object and link it to your target component
    dropTarget = new DropTarget();
    try {
      dropTarget.addDropTargetListener(new DragDropListener());
    }
    catch (TooManyListenersException tmle) {
      // process error
    }
    yourComponent.setDropTarget(dropTarget);
  }
Write a private class for handling the drop action - note that different operating systems receive different file flavours - e.g. windows gets files as a filelist whereas Linux gets a long string
  private class DragDropListener extends DropTargetAdapter {
		
  public void drop(DropTargetDropEvent dtde) {
    Transferable transferable = dtde.getTransferable();
    dtde.acceptDrop(dtde.getDropAction());
    DataFlavor[] flavors = transferable.getTransferDataFlavors();			
    boolean ok = false;
    for (int i=0; i<flavors.length; i++) {
      DataFlavor df = flavors[i];
      if (df.equals(DataFlavor.javaFileListFlavor)) {
        try {
          java.util.List<File> list = (java.util.List<File>)transferable.getTransferData(DataFlavor.javaFileListFlavor);
          for (int j=0; j<list.size(); j++) {
            openFile(list.get(j).getAbsolutePath());
          }
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
        ok = true;
      }
      else if (df.equals(DataFlavor.stringFlavor)) {
        try {
          String file = (String)transferable.getTransferData(DataFlavor.stringFlavor);
          String[] files = file.split(System.getProperty("line.separator"));
          for (int j=0; j<files.length; j++) {
            file = files[j].trim().replace("file://", "");
            openFile(file);
          }
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
        ok = true;
      }
    }
    dtde.dropComplete(true);
    if (!ok) {
      // raise error
    }
  }
PHP : Redirecting Scripts
Created 21 May 2002 00:00
In order to transfer the user to another page programatically, write a location header to the page, for example:
  // turn buffering on, so no other content is written to page before the 
  // redirect headers
  ob_start();
  ..
  ..
  // remove data in buffer and write header
  ob_clean();
  header("Location: mypage.htm");
  exit;
PHP Cookies
Created 23 May 2002 00:00
Setting cookies, is relatively simple, using the following code:
  setcookie("cookiename", $cookie_val, time() + 3600, "", "");
Note: You should set all the above parameters to ensure that the cookie gets written in Internet Explorer 5+. Also, the PHP manual mentions another cookie for secure connections as an additional parameter. For some reason, setting this parameter causes the cookie not to be written to IE 5+, so be sure not to set it.

To retrieve the cookie, use the following:

  $HTTP_COOKIE_VARS["cookiename"]

To delete the cookie:

  // set the cookie expiry date to some time in the past
  setcookie ("cookiename", "", time() - 3600, "", "");
PostgreSQL Sequences
Created 31 May 2002 00:00
Sequence objects (also called sequence generators or just sequences) are special single-row tables created with CREATE SEQUENCE. Sequential fields in tables are linked to sequence objects, so you can insert integers into these fields manually, or leave it up to the sequence object to create the next integer for you.

This is useful when you want the sequential number for further processing - you can manually retrieve the next number in the sequence using

SELECT nextval('sequence_name');
which returns a single-row recordset with one field called "nextval". To insert your new record into the table with the sequential field, just manually insert this value instead of omitting the normally self-incrementing field.