📄 playlistmodel.java
字号:
* @return Node - the previous entry
*/
private Node getPrevious() {
Node nod = currentElement;
// search previous element with tag <record>
while (nod != null) {
nod = nod.getPreviousSibling();
if (nod instanceof Element
&& ((Element) nod).getTagName().equals(RECORD))
return nod;
}
return null;
}
/**
* @see IPlayList#hasNext()
*/
public boolean hasNext() {
return getNext() != null;
}
/**
* @see IPlayList#hasPrevious()
*/
public boolean hasPrevious() {
return getPrevious() != null;
}
/**
* @see IPlayList#getFeature(org.w3c.dom.Node, java.lang.String,
* boolean)
*/
public String getFeature(
String tag) {
return getFeature(currentElement, tag);
}
/**
* @see IPlayList#getFeature(org.w3c.dom.Node, java.lang.String,
* boolean)
*/
public String getFeature(
Object record, String tag) {
if (record == null) return "";
// Find all child elements with specified name
NodeList nl = ((Element) record).getElementsByTagName(tag);
if (nl.getLength() == 0) return "";
// Should be the only one
Node nod = nl.item(0);
// Now get all text child elements and concatenate them
StringBuffer sb = new StringBuffer();
nl = nod.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
if (nl.item(i) instanceof Text)
sb.append(((Text) nl.item(i)).getData());
}
return sb.toString();
}
/**
* @see IPlayList#setFeature(org.w3c.dom.Node, java.lang.String,
* java.lang.String)
*/
public void setFeature(
Object record, String tag, String value) {
// Assuming that playlist elements are XML elements
Element el = (Element) record;
// Get corresponding DOM document
Document doc = el.getOwnerDocument();
NodeList nl = ((Element) record).getElementsByTagName(tag);
// Remove existing elements with same name
for (int i = 0; i < nl.getLength(); i++)
el.removeChild(nl.item(i));
if (value == null || value.length() == 0)
// Deletion of feature - finished.
return;
// Create child element, append to parent, and fill with
// content.
Node nod = doc.createElement(tag);
el.appendChild(nod);
nod.appendChild(doc.createTextNode(value.trim()));
}
/**
* @see IPlayList#setCurrent(org.w3c.dom.Node)
*/
public void setCurrent(
Object current) {
// To avoid event avalanches check if
// the new entry is different from the current entry.
if (currentElement != current) {
// yes, update current entry and notify listeners.
currentElement = (Element) current;
fireSelectionChanged(getSelection());
}
}
/**
* Notify all listeners about change of current entry
*
* @param selection -
* the current entry wrapped into an ISelection instance
*/
private void fireSelectionChanged(
ISelection selection) {
Object[] listeners = selectionChangedListeners
.getListeners();
SelectionChangedEvent event = new SelectionChangedEvent(
this, selection);
for (int i = 0; i < listeners.length; i++)
((ISelectionChangedListener) listeners[i])
.selectionChanged(event);
}
/**
* @see IPlayList#deleteCurrent()
*/
public void deleteCurrent() {
if (currentElement == null) return;
// When deleting the current element position to the next
// element. If this does not exist position to the previous
// element.
Node nod = getNext();
if (nod == null) getPrevious();
// Remove from playlist.
Element playlist = playlistDoc.getDocumentElement();
playlist.removeChild(currentElement);
// Update current element.
setCurrent(nod);
}
/**
* @see IPlayList#insert(java.lang.String, java.lang.String)
*/
public Object insert() {
// Create a new <record> element
Element newRecord = playlistDoc.createElement(RECORD);
// Insert the new element in front of the current
// element...
Element playlist = playlistDoc.getDocumentElement();
playlist.insertBefore(newRecord, currentElement);
// ...and update the current element.
setCurrent(newRecord);
return newRecord;
}
/**
* @see IPlayList#moveDownwards()
*/
public boolean moveDownwards() {
Node next = getNext();
if (next == null) return false;
// If there is a next element, remove the current element
// and insert it again behind the next element.
Element playlist = playlistDoc.getDocumentElement();
playlist.removeChild(currentElement);
playlist.insertBefore(currentElement, next
.getNextSibling());
return true;
}
/**
* @see IPlayList#moveUpwards()
*/
public boolean moveUpwards() {
Node previous = getPrevious();
if (previous == null) return false;
// If there is a previos element, remove the current
// element and insert it again in front of the previos element.
Element playlist = playlistDoc.getDocumentElement();
playlist.removeChild(currentElement);
playlist.insertBefore(currentElement, previous);
return true;
}
/** *** IStructuredContentProvider methods *** */
/**
* @see org.eclipse.jface.viewers.IStructuredContentProvider#
* getElements(java.lang.Object)
*/
public Object[] getElements(
Object inputElement) {
// Fetch all <record> elements from the playlist...
NodeList nl = playlistDoc.getElementsByTagName(RECORD);
// ...and write them into an array
Object[] result = new Object[nl.getLength()];
for (int i = 0; i < result.length; i++)
result[i] = nl.item(i);
return result;
}
/** *** IContentProvider methods *** */
/**
* @see org.eclipse.jface.viewers.IContentProvider#
* inputChanged(org.eclipse.jface.viewers.Viewer,
* java.lang.Object, java.lang.Object)
*
* We trust that this method is called, too, when the application is
* closed.
*/
public void inputChanged(
Viewer viewer, Object oldInput, Object newInput) {
// First save the current playlist
savePlaylist();
if (newInput == null) {
// The application is closed and sets the input to null
if (currentPlaylistFile != null) {
// A playlist was open. Save its name into the INI
// file
try {
OutputStream stream = new FileOutputStream(iniFile);
stream.write(currentPlaylistFile.getBytes());
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
return;
}
if (currentPlaylistFile == null
|| !currentPlaylistFile.equals(newInput))
// Open a new playlist
openPlaylist((String) newInput);
}
/**
* @see org.eclipse.jface.viewers.IContentProvider#dispose()
*/
public void dispose() {}
/** *** ISelectionProvider methods **** */
/**
* @see IPlayList#addSelectionChangedListener(
* org.eclipse.jface.viewers.ISelectionChangedListener)
*/
public void addSelectionChangedListener(
ISelectionChangedListener listener) {
selectionChangedListeners.add(listener);
}
/**
* @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
*/
public ISelection getSelection() {
return new StructuredSelection((currentElement == null)
? new Object[0]
: new Object[]{currentElement});
}
/**
* @see org.eclipse.jface.viewers.ISelectionProvider#
* removeSelectionChangedListener(
* org.eclipse.jface.viewers.ISelectionChangedListener)
*/
public void removeSelectionChangedListener(
ISelectionChangedListener listener) {
selectionChangedListeners.remove(listener);
}
/**
* @see org.eclipse.jface.viewers.ISelectionProvider#
* setSelection(org.eclipse.jface.viewers.ISelection)
*/
public void setSelection(
ISelection selection) {
if (selection instanceof IStructuredSelection) {
Object selected = ((IStructuredSelection) selection)
.getFirstElement();
currentElement = (Element) selected;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -