// 找到一个子字段所从属的字段行 public FieldLine GetFieldLine(SubfieldLine subfield) { int nStart = this.Items.IndexOf(subfield); if (nStart == -1) return null; for (int i = nStart - 1; i >= 0; i--) { EasyLine line = this.Items[i]; if (line is FieldLine) return line as FieldLine; } return null; }
// 插入一个新的子字段 public SubfieldLine NewSubfield(int index) { EasyLine ref_line = this.Items[index]; string strFieldName = ""; if (ref_line is FieldLine) { // 只能后插 TODO: 让对话框不能选择前插 strFieldName = (ref_line as FieldLine).Name; } else { FieldLine ref_field_line = GetFieldLine(ref_line as SubfieldLine); if (ref_field_line == null) return null; // 应该抛出异常? strFieldName = ref_field_line.Name; } // 询问子字段名 NewSubfieldDialog dlg = new NewSubfieldDialog(); dlg.Font = this.Font; dlg.Text = "新子字段"; dlg.AutoComplete = true; dlg.ParentNameString = strFieldName; dlg.NameString = this.DefaultSubfieldName; dlg.MarcDefDom = this.MarcDefDom; dlg.Lang = this.Lang; dlg.StartPosition = FormStartPosition.CenterScreen; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return null; if (ref_line is FieldLine) { // 只能后插 dlg.InsertBefore = false; } string strDefaultValue = ""; List<string> results = null; string strError = ""; // 获得宏值 // parameters: // strSubFieldName 子字段名。特殊地,如果为"#indicator",表示想获取该字段的指示符缺省值 // return: // -1 error // 0 not found // 1 found int nRet = GetDefaultValue( 0, // index, dlg.ParentNameString, dlg.NameString, out results, out strError); if (nRet == -1) MessageBox.Show(this, strError); if (results != null && results.Count > 0) { strDefaultValue = results[0]; } bool bChanged = false; SubfieldLine subfield_line = new SubfieldLine(this); subfield_line.Name = dlg.NameString; subfield_line.Caption = GetCaption(dlg.ParentNameString, dlg.NameString, this._includeNumber); subfield_line.Content = strDefaultValue; if (dlg.InsertBefore == true) { InsertNewLine(index++, subfield_line, false); bChanged = true; } else { index++; InsertNewLine(index++, subfield_line, false); bChanged = true; } if (bChanged == true) this.FireTextChanged(); #if DEBUG this.Verify(); #endif return subfield_line; }
// 插入一个新的字段 public FieldLine NewField(int index) { EasyLine ref_line = null; if (index < this.Items.Count) ref_line = this.Items[index]; if (ref_line is SubfieldLine) { ref_line = GetFieldLine(ref_line as SubfieldLine); if (ref_line == null) { throw new Exception("index 为 " + index.ToString() + " 的子字段行没有找到字段行"); } index = this.Items.IndexOf(ref_line); Debug.Assert(index != -1, ""); } // 询问字段名 NewSubfieldDialog dlg = new NewSubfieldDialog(); GuiUtil.SetControlFont(dlg, this.Font); // dlg.Font = this.Font; dlg.Text = "新字段"; dlg.AutoComplete = true; if (ref_line != null) dlg.NameString = ref_line.Name; else dlg.NameString = this.DefaultFieldName; dlg.MarcDefDom = this.MarcDefDom; dlg.Lang = this.Lang; dlg.StartPosition = FormStartPosition.CenterScreen; dlg.ShowDialog(this); if (dlg.DialogResult != DialogResult.OK) return null; bool bControlField = Record.IsControlFieldName(dlg.NameString); string strDefaultValue = ""; string strIndicator = " "; if (bControlField == false) strDefaultValue = new string((char)31, 1) + "a"; List<string> results = null; string strError = ""; // 获得宏值 // parameters: // strSubFieldName 子字段名。特殊地,如果为"#indicator",表示想获取该字段的指示符缺省值 // return: // -1 error // 0 not found // 1 found int nRet = GetDefaultValue( 0, // index, dlg.NameString, "", out results, out strError); if (nRet == -1) MessageBox.Show(this, strError); if (results != null && results.Count > 0) { strDefaultValue = results[0]; if (bControlField == false) SplitDefaultValue(strDefaultValue, out strIndicator, out strDefaultValue); } bool bChanged = false; // 辅助剖析子字段的对象 MarcField field_node = new MarcField(dlg.NameString, strIndicator, strDefaultValue); FieldLine field_line = new FieldLine(this); field_line.IsControlField = bControlField; field_line.Name = dlg.NameString; field_line.Caption = GetCaption(dlg.NameString, "", this._includeNumber); if (bControlField == false) { field_line.Indicator = strIndicator; // 从 marcdef 中获得缺省的指示符值 field_line.ExpandState = ExpandState.Expanded; } EasyLine after_line = null; #if FIX bool bExpanded = false; bool bHideChanged = false; #endif if (dlg.InsertBefore == true) { after_line = ref_line; } else { // 如果插入点是字段行,需要跳过下属的子字段行 // EasyLine ref_line = this.Items[index]; if (ref_line is FieldLine) { List<EasyLine> lines = GetSubfieldLines(ref_line as FieldLine); index += lines.Count + 1; } else index++; if (index < this.Items.Count) after_line = this.Items[index]; } #if FIX if (after_line != null) { // 如果插入位置后面一个字段是隐藏状态,则会出现故障,需要先修改为显示状态,插入后再隐藏 if (after_line is FieldLine && after_line.Visible == false) { this.ToggleHide(after_line as FieldLine); bHideChanged = true; } // 如果本字段是收缩状态,则会出现故障,需要先修改为展开状态,插入后再收缩 if (after_line.ExpandState == ExpandState.Collapsed) { this.ToggleExpand(after_line); Debug.Assert(after_line.ExpandState == ExpandState.Expanded, ""); bExpanded = true; } } #endif InsertNewLine(index++, field_line, false); bChanged = true; // 如果必要,创建子字段对象 foreach (MarcSubfield subfield_node in field_node.ChildNodes) { SubfieldLine subfield_line = new SubfieldLine(this); subfield_line.Name = subfield_node.Name; subfield_line.Caption = GetCaption(field_node.Name, subfield_node.Name, this._includeNumber); subfield_line.Content = subfield_node.Content; InsertNewLine(index++, subfield_line, false); bChanged = true; } #if FIX // 把参考行恢复到以前的状态 if (after_line != null && bExpanded == true) { this.ToggleExpand(after_line); Debug.Assert(after_line.ExpandState == ExpandState.Collapsed, ""); } if (after_line != null && bHideChanged == true) { this.ToggleHide(after_line as FieldLine); } #endif if (bChanged == true) this.FireTextChanged(); return field_line; }
// 设置 MARC 记录内容 public void SetMarc(string strMarc) { this.Clear(); this.MarcDefDom = null; // 迫使重新获得字段名提示信息 this.DisableUpdate(); try { int nLineIndex = 0; MarcRecord record = new MarcRecord(strMarc); this._header = record.Header.ToString(); // 头标区 foreach (MarcField field in record.ChildNodes) { FieldLine field_line = new FieldLine(this); field_line.Name = field.Name; field_line.Caption = GetCaption(field.Name, "", this._includeNumber); // field_line.Indicator = field.Indicator; InsertNewLine(nLineIndex++, field_line, false); field_line.IsControlField = field.IsControlField; if (field.IsControlField == true) { field_line.Content = field.Content; field_line.ExpandState = ExpandState.None; } else { #if NO // 指示符行 IndicatorLine indicator_line = new IndicatorLine(this); indicator_line.Name = ""; indicator_line.Caption = "指示符"; indicator_line.Content = field.Indicator; InsertNewLine(nLineIndex++, indicator_line); #endif field_line.Indicator = field.Indicator; foreach (MarcSubfield subfield in field.ChildNodes) { SubfieldLine subfield_line = new SubfieldLine(this); subfield_line.Name = subfield.Name; subfield_line.Caption = GetCaption(field.Name, subfield.Name, this._includeNumber); subfield_line.Content = subfield.Content; InsertNewLine(nLineIndex++, subfield_line, false); } field_line.ExpandState = ExpandState.Expanded; } } this.Changed = false; ResetLineState(); } finally { this.EnableUpdate(); } }